Azure存储CORS

vin*_*ags 2 javascript azure azure-storage-blobs cors

我在www.somedomain.com上有一个申请表.现在我的所有文件(上载的最终用户)都存储在Azure存储上,该存储有一个像somesubdomain.blob.core.windows.net这样的域.每当用户想要查看文档时,文档在azure上的公共链接被添加到iframe源并且可以被查看.唯一的问题是,在许多情况下,该文件是一个包含Javascript的html,它试图访问最初在我的第一个主机上的父进程上的一些基本安全自由变量.

每当天蓝色存储上的html文件试图访问父文档变量时,我都会收到错误"阻止带有源的帧http://somesubdomain.blob.core.windows.net "来访问带有源的帧" http:http: //somedomain.com ".协议,域名和端口必须匹配."

任何有关这方面的指导和帮助都会有所帮助.

Pie*_*eau 6

在Azure存储帐户上启用CORS的最简单方法是使用azure-cli

npm i azure-cli -g

然后,可以通过命令行配置CORS:

azure storage cors set
  -a "storage-account"
  -k "storage-account-key"
  --blob/table/queue/file
  --cors "[{\"AllowedOrigins\":\"*\",\"AllowedMethods\":\"GET\",\"MaxAgeInSeconds\":\"86400\",\"AllowedHeaders\":\"*\",\"ExposedHeaders\":\"*\"}]"
Run Code Online (Sandbox Code Playgroud)


Gau*_*tri 5

您需要在存储帐户的Blob服务上启用CORS以进行跨域JavaScript访问.您可以在此处了解有关Azure存储和CORS的更多信息:https://msdn.microsoft.com/en-us/library/azure/dn535601.aspx.

我前段时间也写了一篇博文,您可以在这里阅读:http://gauravmantri.com/2013/12/01/windows-azure-storage-and-cors-lets-have-some-fun /.

If you're using .Net Storage Client library, you can use code below to set CORS rule:

static void AddCorsRuleStorageClientLibrary()
{
    //Add a new rule.
    var corsRule = new CorsRule()
    {
        AllowedHeaders = new List<string> { "*" },
        AllowedMethods = CorsHttpMethods.Get
        AllowedOrigins = new List<string> { "http://somedomain.com" },//This is the URL of your application.
        MaxAgeInSeconds = 1 * 60 * 60,//Let the browser cache it for an hour
    };

    //First get the service properties from storage to ensure we're not adding the same CORS rule again.
    var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
    var client = storageAccount.CreateCloudBlobClient();
    var serviceProperties = client.GetServiceProperties();
    var corsSettings = serviceProperties.Cors;

    corsSettings.CorsRules.Add(corsRule);
    //Save the rule
    client.SetServiceProperties(serviceProperties);
}
Run Code Online (Sandbox Code Playgroud)