我在azure存储中创建了几个容器,并将一些文件上传到这些容器中.现在我需要给容器/ blob提供域级访问权限.所以我从代码级别尝试了它,如下所示.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); ServiceProperties blobServiceProperties = new ServiceProperties(); blobServiceProperties.Cors.CorsRules.Add(new CorsRule(){ AllowedHeaders = new List<string>() {"*"},ExposedHeaders = new List<string>() {"*"},AllowedMethods = CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Delete,AllowedOrigins = new List<string>() { "http://localhost:8080/"},MaxAgeInSeconds = 3600,}); blobClient.SetServiceProperties(GetBlobServiceProperties());
但是,如果我从代码创建所有内容,上面的代码似乎是有效的(如果我错了,请纠正我).我也找到类似于Here的设置,
<CorsRule> <AllowedOrigins>http://www.contoso.com,http://www.fabrikam.com</AllowedOrigins> <AllowedMethods>PUT,GET</AllowedMethods> <AllowedHeaders>x-ms-@R_403_338@-data*,x-ms-@R_403_338@-target,x-ms-@R_403_338@-source</AllowedHeaders> <ExposedHeaders>x-ms-@R_403_338@-*</ExposedHeaders> <MaxAgeInSeconds>200</MaxAgeInSeconds> </CorsRule>
但我没有得到这个代码必须放在哪里.我的意思是在哪个文件中.或者从azure门户创建容器或blob时是否有任何CORS设置.请协助.任何帮助都会很明显.谢谢!
解决方法
以下回答了标题中实际提出的问题.似乎提问者已经知道如何在他的代码中做到这一点,但这是我对此的回答.不幸的是,MS推出的代码示例远非容易或清晰,所以我希望这有助于其他人.在此解决方案中,您只需要一个CloudStorageAccount实例,您可以从该实例调用此函数(作为扩展方法).
//用法:
// -- example usage (in this case adding a wildcard CORS rule to this account -- CloudStorageAccount acc = getYourStorageAccount(); acc.SetCORSPropertiesOnBlobService(cors => { var wildcardRule = new CorsRule() { AllowedMethods = CorsHttpMethods.Get,AllowedOrigins = { "*" } }; cors.CorsRules.Add(wildcardRule); return cors; });
//代码:
/// <summary> /// Allows caller to replace or alter the current CorsProperties on a given CloudStorageAccount. /// </summary> /// <param name="storageAccount">Storage account.</param> /// <param name="alterCorsRules">The returned value will replace the /// current ServiceProperties.Cors (ServiceProperties) value. </param> public static void SetCORSPropertiesOnBlobService(this CloudStorageAccount storageAccount,Func<CorsProperties,CorsProperties> alterCorsRules) { if (storageAccount == null || alterCorsRules == null) throw new ArgumentNullException(); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); ServiceProperties serviceProperties = blobClient.GetServiceProperties(); serviceProperties.Cors = alterCorsRules(serviceProperties.Cors) ?? new CorsProperties(); blobClient.SetServiceProperties(serviceProperties); }
考虑CorsRule类的属性可能会有所帮助:
CorsRule corsRule = new CorsRule() { AllowedMethods = CorsHttpMethods.Get,// Gets or sets the HTTP methods permitted to execute for this origin AllowedOrigins = { "*" },// (IList<string>) Gets or sets domain names allowed via CORS. //AllowedHeaders = { "*" },// (IList<string>) Gets or sets headers allowed to be part of the CORS request //ExposedHeaders = null,// (IList<string>) Gets or sets response headers that should be exposed to client via CORS //MaxAgeInSeconds = 33333 // Gets or sets the length of time in seconds that a preflight response should be cached by browser };