2015-08-23 211 views
2

我在www.somedomain.com上有一个应用程序。现在,我所有的文件(上传的最终用户)都存储在具有像somesubdomain.blob.core.windows.net这样的域的Azure存储上。无论何时用户想要查看文档,azure上文档的公共链接都会添加到iframe源文件中,并且可以查看。唯一的问题是,在许多情况下,该文件是一个带Javascript包含的html,它试图访问最初在我的第一个主机上的父级上的一些基本安全免费变量。Azure存储CORS

每当azure存储上的html文件尝试访问父文档变量时,我都会收到错误“阻止原始帧'http://somesubdomain.blob.core.windows.net'访问原始帧”http://somedomain.com“的帧。必须使用协议,域和端口比赛。'

任何指导和帮助的,这将是有益的

+0

您是否为您的blob存储配置了CORS规则? –

+0

我该怎么做。我检查了天蓝色的文档,并没有发现它非常有用。谢谢 – vinayjags

回答

5

您需要启用您的存储帐户的blob服务跨域的JavaScript访问CORS您可以了解更多关于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/

如果您使用的.Net存储客户端库,您可以使用下面的代码来设置CORS规则:

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

我有一个问题,在哪里把这个代码,当我打电话? –

+0

所以你不要把这段代码放到你的应用程序中。你所做的是创建一个单独的应用程序来设置CORS规则,或者你可以使用任何能够管理CORS规则的存储浏览器。 –

0

解决的另一个方法是创建一个指向您的存储文件的自定义域 - 类似filestore.somedomain.com。

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\":\"*\"}]" 
+0

你好,我在'azure-cli'上做了这个,在命令行上它说一切正常。但错误'对预检请求的响应不通过访问控制检查:否请求的资源上存在'Access-Control-Allow-Origin'标头。原因'http:// localhost:3210'因此不被允许访问。该响应具有HTTP状态码403。“仍然存在。 – CENT1PEDE

+0

顺便说一下,我做了''AllowedHeaders':“*”' – CENT1PEDE

+0

预检请求是'OPTIONS',你需要指定''AllowedMethods“:”GET,OPTIONS“'以及你正在使用的其他方法。 –

1

这里的类似于Pier-Luc Gendreau的答案,但与新的azure-cli v2.0相关。

az storage cors add --account-name $ACCNT_NAME --account-key $ACCNT_KEY \ 
    --methods GET --origins '*' --services t --allowed-headers '*' 

注意,V2.0是基于Python与之相对应的是基于Node.js的的v1.0

官方安装指令可here,但对我来说,下面似乎是一个更好的选择,以保持系统清洁:

virtualenv --system-site-packages -p python3 ~/azure-cli/ 
source ~/azure-cli/bin/activate 
pip3 install azure-cli 

下面是有关你可能想所需参数的帮助信息的提取物改变你的具体情况。

--methods [Required]: List of HTTP methods allowed to be executed by the origin. Allowed 
         values: DELETE, GET, HEAD, MERGE, OPTIONS, POST, PUT. 
--origins [Required]: List of origin domains that will be allowed via CORS, or "*" to allow all 
         domains. 
--services [Required]: The storage service(s) to add rules to. Allowed options are: (b)lob, 
         (f)ile, (q)ueue, (t)able. Can be combined.