小编典典

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

ajax

我们在Windows Azure上有一个Blob存储。

http://mytest.blob.core.windows.net/forms

我使用CloudBerry将一些文件上传到存储。而且我可以通过浏览器成功下载文件。这些文件是简单的文本文件,但是具有不同的文件扩展名。例如,

http://mytest.blob.core.windows.net/forms/f001.etx

我想通过jquery($ .get)下载文件,但是由于CORS失败。

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

而且,我也应该在客户端为CORS做些事情吗?


阅读 399

收藏
2020-07-26

共1个答案

小编典典

更新:
在回答此问题时,Azure门户没有此功能。现在按此处概述进行操作。下面概述了添加UI之前执行此操作的方法。

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

如果愿意,可以始终以编程方式设置BORS存储的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
     });
}

如果您正在寻找执行此操作的工具,则一些存储浏览器支持配置CORS-Azure存储资源管理器,Cerebrata Azure ManagementStudio,Cloud Portam(公开-我正在构建Cloud Portam实用程序)。

正确配置CORS之后,您可以使用Rory的答案中提到的代码从blob存储中下载文件。正如Rory所说,您不必在客户端做任何特殊的事情。

2020-07-26