我们在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做些什么?
答案 0 :(得分:22)
答案 1 :(得分:16)
更新:在此回答时,Azure门户网站没有此功能。它现在以outlined here为准。以下概述了在添加UI之前执行此操作的方法。
如何在Portal中的Azure BLOB存储中配置CORS?
如果需要,可以始终以编程方式为blob存储设置CORS规则。如果您使用的是.Net Storage Client库,请查看存储团队发布的这篇博文:http://blogs.msdn.com/b/windowsazurestorage/archive/2014/02/03/windows-azure-storage-introducing-cors.aspx。从该博客文章设置CORS设置的代码:
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管理工作室,云端口(披露 - 我正在构建云端口实用程序)。 / p>
正确配置CORS后,您可以使用Rory答案中提到的代码从blob存储中下载文件。如Rory所述,您不必在客户端做任何特殊的事情。
答案 2 :(得分:5)
现在,您可以使用azure power shell轻松设置/编辑/查看CORS规则。有关此链接的更多信息:
https://azure.microsoft.com/en-us/documentation/articles/storage-powershell-guide-full/
总结以下power shell命令将为您的blob设置CORS:
handlePostSubmit: function(post) {
var posts = this.state.data;
post.id = Date.now();
var newPosts = posts.concat([post]);
this.setState({data: newPosts});
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: post,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
this.setState({data: posts});
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
delc: function(post){
var index = this.state.data.indexOf(post);
var newData = React.addons.update(this.state.data, { $splice: [[index, 1]] });
this.setState({ data: newData });
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'DELETE',
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
this.setState({data: newData});
console.error(this.props.url, status, err.toString());
}.bind(this)
});
}
以登录您的帐户Add-AzureAccount
Get-AzureSubscription |
Format-Table SubscriptionName, IsDefault, IsCurrent,
CurrentStorageAccountName
$SubscriptionName = 'Your subscription
name'
Get-AzureStorageBlob
创建授权上下文并输入所需的参数。$ctx =
New-AzureStorageContext
Get-AzureStorageCORSRule -ServiceType Blob
-Context $ctx
$CorsRules = (@{
AllowedHeaders=@("*");
AllowedOrigins=@("*");
ExposedHeaders=@("content-length");
MaxAgeInSeconds=200;
AllowedMethods=@("Get","Connect", "Head")})
答案 3 :(得分:3)
通过PowerShell设置CORS的更简洁方法: https://gist.github.com/irwinwilliams/4cf93b6e2461c753ff125590650186ae
#works with Azure in Powershell v 1.3.2
clear
$StorageAccountName = "[storageaccountname]"
$Key = "[storageaccountkey]"
$Context = New-AzureStorageContext -StorageAccountKey $Key -StorageAccountName $StorageAccountName
$CorsRules = (@{
AllowedHeaders=@("*");
AllowedOrigins=@("*");
ExposedHeaders=@("content-length");
MaxAgeInSeconds=200;
AllowedMethods=@("Get","Connect", "Head")})
Set-AzureStorageCORSRule -ServiceType Blob -CorsRules $CorsRules -Context $Context
$CORSrule = Get-AzureStorageCORSRule -ServiceType Blob -Context $Context
echo "Current CORS rules: "
echo $CORSrule
答案 4 :(得分:2)
答案 5 :(得分:1)
为了确保您的B2C自定义工作,您需要处理以下事项:
提示:要验证您托管内容的网站是否已启用CORS并测试CORS请求,您可以使用网站http://test-cors.org/。多亏了这个站点,您可以简单地将CORS请求发送到远程服务器(以测试是否支持CORS),或者将CORS请求发送到测试服务器(以探索CORS的某些功能)。
答案 6 :(得分:1)
答案 7 :(得分:0)
Azure Blob存储支持CORS,但您需要在发出请求之前设置标头。要做到这一点,最好使用$.ajax
,因为它可以让您更好地控制发送的信息。这是this demo的重新编写的例子:
function setHeader(xhr) {
xhr.setRequestHeader('x-ms-version', '2013-08-15');
xhr.setRequestHeader('MaxDataServiceVersion', '3.0');
xhr.setRequestHeader('Accept', 'application/json;odata=nometadata');
}
$.ajax({
type: 'GET',
datatype: "json",
url: 'http://mytest.blob.core.windows.net/forms/f001.etx',
beforeSend: setHeader,
success: function(data) {
// do something with the retrieved file.
},
error: function (res, status, xhr) {
alert("can't get the data for the specified table");
}
});
答案 8 :(得分:0)
这就是我使用控制台应用程序启用cors的方法,只需在StorageCredentials中提供您的凭据:
private static CloudStorageAccount StorageAccount;
public static CloudBlobClient BlobClient
{
get;
private set;
}
static void Main(string[] args)
{
StorageAccount = new CloudStorageAccount(new StorageCredentials("AccountName", "AccountKey"), true);
BlobClient = StorageAccount.CreateCloudBlobClient();
InitializeCors(BlobClient);
}
private static void InitializeCors(CloudBlobClient blobClient)
{
ServiceProperties blobServiceProperties = BlobClient.GetServiceProperties();
ConfigureCors(blobServiceProperties);
BlobClient.SetServiceProperties(blobServiceProperties);
}
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
});
}