Zac*_*ach 15 ajax jquery azure cors
我们在Windows Azure上有一个blob存储.
http://mytest.blob.core.windows.net/forms
Run Code Online (Sandbox Code Playgroud)
我使用CloudBerry将一些文件上传到存储.我可以通过浏览器成功下载文件.这些文件是简单的文本文件,但具有不同的文件扩展名.例如,
http://mytest.blob.core.windows.net/forms/f001.etx
Run Code Online (Sandbox Code Playgroud)
我想通过jquery($ .get)下载文件,但由于CORS失败了.
如何在Portal中的Azure BLOB存储中配置CORS?
而且,我是否应该在客户端为CORS做些什么?
Gau*_*tri 19
更新:在此答复时,Azure门户没有此功能.它现在如此处所述.以下概述了在添加UI之前执行此操作的方法.
如何在Portal中的Azure BLOB存储中配置CORS?
如果需要,可以始终以编程方式为blob存储设置CORS规则.如果您正在使用.Net Storage Client库,请查看存储团队的这篇博客文章:http://blogs.msdn.com/b/windowsazurestorage/archive/2014/02/03/windows-azure-storage-introducing- cors.aspx.从该博客文章设置CORS设置的代码:
private static void InitializeCors()
{
// CORS should be enabled once at service startup
// Given a BlobClient, download the current Service Properties
ServiceProperties blobServiceProperties = BlobClient.GetServiceProperties();
ServiceProperties tableServiceProperties = TableClient.GetServiceProperties();
// Enable and Configure CORS
ConfigureCors(blobServiceProperties);
ConfigureCors(tableServiceProperties);
// Commit the CORS changes into the Service Properties
BlobClient.SetServiceProperties(blobServiceProperties);
TableClient.SetServiceProperties(tableServiceProperties);
}
private static void ConfigureCors(ServiceProperties serviceProperties)
{
serviceProperties.Cors = new CorsProperties();
serviceProperties.Cors.CorsRules.Add(new CorsRule()
{
AllowedHeaders = new List<string>() { "*" },
AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post,
AllowedOrigins = new List<string>() { "*" },
ExposedHeaders = new List<string>() { "*" },
MaxAgeInSeconds = 1800 // 30 minutes
});
}
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找相同的工具,一些存储资源管理器支持配置CORS - Azure存储资源管理器,Cerebrata Azure管理工作室,云端口(披露 - 我正在构建云端口实用程序).
正确配置CORS后,您可以使用Rory答案中提到的代码从blob存储中下载文件.如Rory所述,您无需在客户端执行任何特殊操作.
小智 5
现在,您可以使用azure power shell轻松设置/编辑/查看CORS规则.有关此链接的更多信息:
https://azure.microsoft.com/en-us/documentation/articles/storage-powershell-guide-full/
总结以下power shell命令将为您的blob设置CORS:
Add-AzureAccount
以登录您的帐户Get-AzureSubscription |
Format-Table SubscriptionName, IsDefault, IsCurrent,
CurrentStorageAccountName
$SubscriptionName = 'Your subscription
name'
Get-AzureStorageBlob
$ctx =
New-AzureStorageContext
并输入所需的参数.Get-AzureStorageCORSRule -ServiceType Blob
-Context $ctx
$CorsRules = (@{
AllowedHeaders=@("*");
AllowedOrigins=@("*");
ExposedHeaders=@("content-length");
MaxAgeInSeconds=200;
AllowedMethods=@("Get","Connect", "Head")})
Set-AzureStorageCORSRule -ServiceType Blob -CorsRules $CorsRules
-Context $ctx
归档时间: |
|
查看次数: |
14214 次 |
最近记录: |