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

How can I set CORS in Azure BLOB Storage in Portal?(如何在门户的 Azure BLOB 存储中设置 CORS?)
本文介绍了如何在门户的 Azure BLOB 存储中设置 CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我们在 Windows Azure 上有一个 blob 存储.

We have a blob storage on Windows Azure.

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

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

I uploaded a few files to the storage using CloudBerry. And I can download the files by browsers successfully. These files are simple text files, but with different file extensions. For example,

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

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

I want to download the files via jquery ($.get), however, it failed because of CORS.

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

How can I configure CORS in Azure BLOB Storage in Portal?

而且,我也应该在客户端为 CORS 做点什么吗?

And, should I do something for CORS in the client side too?

推荐答案

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

UPDATE: At the time of this answer the Azure Portal did not have this feature. It does now as outlined here. The following outlines the way to do this before the UI was added.

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

How can I configure CORS in Azure BLOB Storage in Portal?

如果您愿意,您可以随时以编程方式设置 Blob 存储的 CORS 规则.如果您使用的是 .Net 存储客户端库,请查看存储团队的这篇博文:http://blogs.msdn.com/b/windowsazurestorage/archive/2014/02/03/windows-azure-storage-introducing-cors.aspx.从该博客文章中设置 CORS 设置的代码:

If you want you can always set the CORS rules for blob storage programmatically. If you're using .Net Storage Client library, check out this blog post from storage team: http://blogs.msdn.com/b/windowsazurestorage/archive/2014/02/03/windows-azure-storage-introducing-cors.aspx. Code for setting CORS setting from that blog post:

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 Management Studio、Cloud Portam(披露 - 我正在构建 Cloud Portam 实用程序).

If you're looking for a tool to do the same, a few storage explorers have support for configuring CORS - Azure Storage Explorer, Cerebrata Azure Management Studio, Cloud Portam (Disclosure - I'm building Cloud Portam utility).

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

Once the CORS is configured properly, you can use the code mentioned in Rory's answer to download the file from blob storage. You don't have to do anything special on the client side as mentioned by Rory.

这篇关于如何在门户的 Azure BLOB 存储中设置 CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
How do I get the HTTP status code with jQuery?(如何使用 jQuery 获取 HTTP 状态码?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)
WebKit quot;Refused to set unsafe header #39;content-length#39;quot;(WebKit “拒绝设置不安全的标头‘内容长度’)
$.ajax call working fine in IE8 and Doesn#39;t work in firefox and chrome browsers($.ajax 调用在 IE8 中运行良好,但在 Firefox 和 chrome 浏览器中不起作用)