gpt4 book ai didi

c# - 获取 Azure blob 容器

转载 作者:行者123 更新时间:2023-12-02 06:01:54 25 4
gpt4 key购买 nike

我正在尝试获取一个容器(如果存在),如果不存在,则创建它。

我感到困惑的是,GetBlobContainersAsync 返回一个 BlobContainerItem,而 CreateBlobContainerAsync 返回一个 BlobContainerClient

当我找到容器时,如何从 BlobContainerItem 获取 BlobContainerClient

这是我到目前为止所拥有的:

var blobServiceClient = new BlobServiceClient(this.ConnectionString);

BlobContainerItem archiveContainer = null;
await foreach (var container in blobServiceClient.GetBlobContainersAsync(prefix: Uploader.ContainerName))
{
if (String.Compare(container.Name, Uploader.ContainerName,
CultureInfo.CurrentCulture, CompareOptions.Ordinal) == 0)
{
archiveContainer = ???
break;
}
}

if (archiveContainer == null)
{
archiveContainer = await blobServiceClient.CreateBlobContainerAsync(Uploader.ContainerName);
}

最佳答案

您实际上不必执行所有这些操作。

只需创建 BlobContainerClient 的实例使用连接字符串和容器名称,然后调用 CreateIfNotExistsAsync 在上面。如果容器不存在,此方法将创建该容器。

来自文档:

The CreateIfNotExistsAsync(PublicAccessType, IDictionary<String,String>, BlobContainerEncryptionScopeOptions, CancellationToken) operation creates a new container under the specified account.
If the container with the same name already exists, it is not changed.

类似于:

var blobContainerClient = new BlobContainerClient(this.ConnectionString, Uploader.ContainerName);
await blobContainerClient.CreateIfNotExistsAsync();

关于c# - 获取 Azure blob 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61859399/

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