如何在Portal中的Azure BLOB存储中设置CORS?

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做些什么?

Ily*_*dik 25

现在可以直接在门户网站上做到这一点.如果您只选择帐户,您将看到带有各种选项的菜单,CORS将成为Blob,File等每项服务的其中一个.

在此输入图像描述 在此输入图像描述


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所述,您无需在客户端执行任何特殊操作.

  • 它最终在用户界面中可用,请参阅下面的答案http://stackoverflow.com/a/41351674/1671558 (2认同)

Saj*_*ran 13

使用 Azure 门户中的新界面,您只需导航到存储帐户即可启用 CORS

在此输入图像描述

然后启用 CORS 并进行必要的设置

在此输入图像描述


小智 6

如果您想将 blob 存储 JSON 文件作为 rest API 访问,那么您应该从存储帐户启用 CORS。进入存储帐户 > CORS > Blob 服务 >然后设置所有必需的值。 在此处输入图片说明


小智 5

现在,您可以使用azure power shell轻松设置/编辑/查看CORS规则.有关此链接的更多信息:

https://azure.microsoft.com/en-us/documentation/articles/storage-powershell-guide-full/

总结以下power shell命令将为您的blob设置CORS:

  1. 运行Add-AzureAccount以登录您的帐户
  2. 在azure中查看您的订阅 Get-AzureSubscription | Format-Table SubscriptionName, IsDefault, IsCurrent, CurrentStorageAccountName
  3. 设置所需的订阅 $SubscriptionName = 'Your subscription name'
  4. 检查你想要的斑点 Get-AzureStorageBlob
  5. 现在,您需要为blob创建授权上下文$ctx = New-AzureStorageContext并输入所需的参数.
  6. 您现在已准备好为您的blob获取或设置CORS规则.检查当前的CORS规则Get-AzureStorageCORSRule -ServiceType Blob -Context $ctx
  7. 设置当前的CORS规则,例如: $CorsRules = (@{ AllowedHeaders=@("*"); AllowedOrigins=@("*"); ExposedHeaders=@("content-length"); MaxAgeInSeconds=200; AllowedMethods=@("Get","Connect", "Head")})
  8. Set-AzureStorageCORSRule -ServiceType Blob -CorsRules $CorsRules -Context $ctx