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

查看:36
本文介绍了如何在门户中的 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.

如何在门户中的 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.

如何在门户中的 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 管理工作室、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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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