获取Azure Blob容器

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

My confusion is because GetBlobContainersAsync returns a BlobContainerItem and CreateBlobContainerAsync returns a BlobContainerClient.

When I find the container, how can I get the BlobContainerClient from the BlobContainerItem?

这是我到目前为止的内容:

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);
}
评论
bquos
bquos

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

Simply create an instance of BlobContainerClient using the connection string and container name and then call CreateIfNotExistsAsync on it. This method will create the container if it does not exist. From the documentation:

CreateIfNotExistsAsync(PublicAccessType,   IDictionary,BlobContainerEncryptionScopeOptions,   CancellationToken)操作在   指定的帐户。如果同名容器已经存在,   它没有改变。

就像是:

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