gpt4 book ai didi

azure - 使用 Azure.Storage.Blobs 设置 CORS 规则

转载 作者:行者123 更新时间:2023-12-03 04:57:43 26 4
gpt4 key购买 nike

我正在尝试从已弃用的 Microsoft.WindowsAzure.Storage 迁移到 Azure.Storage。在我的 API 应用程序中,我偶尔会调用一个方法,以编程方式在我的 Azure 存储帐户中设置 CORS 规则。

如何使用新的 Azure.Storage.BlobsCORS 规则添加到属性?

我在 Microsoft.WindowsAzure.Storage 下工作的原始代码如下。在以下代码中,_clientCloudBlobClient 的实例。我知道在 Azure.Storage.Blobs 中,我需要使用 BlobServiceClient 我现在正在这样做,但正如我所说,以下代码的某些部分不起作用,因为某些方法/属性不再存在。我确信它们已转移到其他地方,但我一直无法弄清楚在哪里。

public async Task ConfigureCors()
{
var ALLOWED_CORS_ORIGINS = new List<String> { "http://localhost:49065", "https://myappdomain.com", "https://www.myappdomain", "https://login.microsoftonline.com" };
var ALLOWED_CORS_HEADERS = new List<String> { "x-ms-meta-qqfilename", "Content-Type", "x-ms-blob-type", "x-ms-blob-content-type" };
const CorsHttpMethods ALLOWED_CORS_METHODS = CorsHttpMethods.Get | CorsHttpMethods.Delete | CorsHttpMethods.Put | CorsHttpMethods.Options;
const int ALLOWED_CORS_AGE_DAYS = 5;

var properties = await _client.GetServicePropertiesAsync();

properties.DefaultServiceVersion = "2013-08-15";
await _client.SetServicePropertiesAsync(properties);

var addRule = true;
if (addRule)
{
var ruleWideOpenWriter = new CorsRule()
{
AllowedHeaders = ALLOWED_CORS_HEADERS,
AllowedOrigins = ALLOWED_CORS_ORIGINS,
AllowedMethods = ALLOWED_CORS_METHODS,
MaxAgeInSeconds = (int)TimeSpan.FromDays(ALLOWED_CORS_AGE_DAYS).TotalSeconds
};
properties.Cors.CorsRules.Clear();
properties.Cors.CorsRules.Add(ruleWideOpenWriter);
await _client.SetServicePropertiesAsync(properties);
}
}

看起来我可以通过将 _client.GetServicePropertiesAsync() 更改为 _client.GetPropertiesAsync() 来获取和设置属性,但 DefaultServiceVersion 不再是那里。另外,我似乎找不到设置 CORS 规则的正确方法。

非常感谢您的建议。谢谢!

最佳答案

使用Azure.Storage.Blobs时可以使用下面的代码(我使用的是同步方法,如果需要,请将其更改为异步方法):

        var properties = blobServiceClient.GetProperties().Value;

properties.DefaultServiceVersion = "xxx";

BlobCorsRule rule = new BlobCorsRule();
rule.AllowedHeaders= "x-ms-meta-qqfilename,Content-Type,x-ms-blob-type,x-ms-blob-content-type";
rule.AllowedMethods = "GET,DELETE,PUT,OPTIONS";
rule.AllowedOrigins = "http://localhost:49065,https://myappdomain.com,https://www.myappdomain,https://login.microsoftonline.com";
rule.MaxAgeInSeconds = 3600; // in seconds

properties.Cors.Add(rule);

blobServiceClient.SetProperties(properties);

关于azure - 使用 Azure.Storage.Blobs 设置 CORS 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64884109/

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