問題描述
我們在 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!