如何在Portal的Azure BLOB存储中设置CORS? [英] How can I set CORS in Azure BLOB Storage in Portal?

查看:140
本文介绍了如何在Portal的Azure BLOB存储中设置CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在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做些什么吗?

解决方案

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

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

如果需要,您可以始终以编程方式设置BORS存储的CORS规则.如果您使用的是.Net Storage Client库,请从存储团队中查看此博客文章:

如果您正在寻找一种工具来执行相同的操作,则一些存储浏览器支持配置CORS-Azure存储资源管理器,Cerebrata Azure Management Studio,Cloud Portam(公开-我正在构建Cloud Portam实用程序). /p>

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

We have a blob storage on Windows Azure.

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

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

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

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

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

解决方案

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.

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

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
     });
}

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).

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆