如何在Azure BLOB Storage中通过Portal设置CORS?
如何在Azure BLOB Storage中通过Portal设置CORS?
我们在Windows Azure上有一个Blob存储。\n
http://mytest.blob.core.windows.net/forms
\n我使用CloudBerry上传了一些文件到存储中,并且可以通过浏览器成功下载这些文件。\n这些文件是简单的文本文件,但具有不同的文件扩展名。\n例如,\n
http://mytest.blob.core.windows.net/forms/f001.etx
\n我想通过jquery ($.get)下载这些文件,但由于CORS的原因失败了。\n我该如何在Azure BLOB存储中的门户中配置CORS?\n此外,我还需要在客户端做一些CORS相关的工作吗?
问题出现的原因是需要将Azure BLOB Storage设置为允许跨域资源共享(CORS)以便以REST API的方式访问JSON文件。为了解决这个问题,需要按照以下步骤进行设置:
1. 进入Azure Portal并登录。
2. 导航到存储帐户,找到需要设置CORS的存储帐户。
3. 进入存储帐户后,找到菜单栏中的"CORS"选项,并点击进入。
4. 在"CORS"页面中,找到"Blob service"部分。
5. 在"Blob service"部分中,设置所需的值。
6. 可以设置以下属性:
- Allowed origins:允许访问该存储帐户的来源URL列表。
- Allowed methods:允许的HTTP方法列表,例如GET、PUT、POST等。
- Allowed headers:允许的请求头列表。
- Exposed headers:允许客户端访问的响应头列表。
- Max age:预检请求的最大缓存时间。
7. 设置完所有的值后,保存设置。
8. 现在,可以以REST API的方式访问JSON文件了。
以上是在Azure Portal中设置CORS的步骤,通过这些步骤可以解决在Azure BLOB Storage中设置CORS的问题。
如何在Azure BLOB Storage中设置CORS?这个问题的出现是因为在Azure Portal中没有这个功能。但是,在UI添加之前,可以通过编程的方式设置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应该在服务启动时启用 // 给定BlobClient,下载当前的服务属性 ServiceProperties blobServiceProperties = BlobClient.GetServiceProperties(); ServiceProperties tableServiceProperties = TableClient.GetServiceProperties(); // 启用和配置CORS ConfigureCors(blobServiceProperties); ConfigureCors(tableServiceProperties); // 将CORS更改提交到服务属性 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() { "*" }, AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post, AllowedOrigins = new List () { "*" }, ExposedHeaders = new List () { "*" }, MaxAgeInSeconds = 1800 // 30 minutes }); }
如果你正在寻找一个可以执行相同操作的工具,一些存储资源管理器支持配置CORS,如Azure Storage Explorer、Cerebrata Azure Management Studio和Cloud Portam。配置CORS正确之后,你可以使用Rory's回答中提到的代码从blob存储中下载文件。你不需要在客户端做任何特殊处理。
谢谢,我将寻找工具。我刚试过AzureStorageExplorer,它无法将*设置为AllowedOrgin字段。非常奇怪!尝试其他工具...
这个问题不能通过门户解决,是不是很奇怪?
有用的回答,但是是否可以按容器启用CORS?
不可以。请参考我的回答,了解原因:stackoverflow.com/questions/36543488/…。
在UI中终于可以了,可以查看我的答案:stackoverflow.com/a/41351674/1671558。
问题的原因是在Azure BLOB Storage中设置CORS时遇到了一些问题。解决方法是通过Azure门户直接进行设置。只需选择相应的账户,就会看到包含各种选项的菜单,其中包括每个服务(如Blob、File等)的CORS设置。
需要注意的是,Azure在最大期限属性过期后会重置此设置。
有用户遇到了相同的问题,但添加了CORS设置后仍无法使用get命令,仍然显示以下错误信息:"Failed to load xxx.jpg: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:4650' is therefore not allowed access."
另外,有用户在门户中尝试保存此规则时遇到了错误,错误信息如下:"Failed to save CORS rules for 1 out of 1 services Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature."