gpt4 book ai didi

c# - 如何在 Windows Azure 中为 Blob 存储配置 CORS 设置

转载 作者:太空狗 更新时间:2023-10-29 20:56:25 25 4
gpt4 key购买 nike

我在 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-meta-data*,x-ms-meta-target,x-ms-meta-source</AllowedHeaders>
<ExposedHeaders>x-ms-meta-*</ExposedHeaders>
<MaxAgeInSeconds>200</MaxAgeInSeconds>
</CorsRule>

但是我不知道这段代码应该放在哪里。我的意思是在哪个文件中。或者从 azure 门户创建容器或 blob 时是否有任何 CORS 设置。请协助。任何帮助将不胜感激。谢谢!

最佳答案

下面回答了标题中实际提出的问题。看来提问者已经知道如何在很大程度上从他的代码中做到这一点,但这是我对此的回答。不幸的是,微软发布的代码示例远非简单或清晰,所以我希望这对其他人有帮助。在此解决方案中,您需要的只是一个 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
};

关于c# - 如何在 Windows Azure 中为 Blob 存储配置 CORS 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com