如何在门户的 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在门户的 Azure BLOB 存储中设置 CORS?

基础教程推荐