如何检查Azure Blob Storage V12中是否存在容器 [英] How do you check that a container exists in Azure Blob Storage V12
本文介绍了如何检查Azure Blob Storage V12中是否存在容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以前使用Azure Blob Storage SDK V11时,如果要创建容器但不确定容器是否存在,可以使用CreateIfNotExists。
但是,在V12版本中,CreateIfNotExists不再可用,我从Microsoft找到的唯一示例就是简单地创建一个容器,而不检查它是否已经存在。
那么,有没有人知道V12中在尝试创建容器之前检查它是否存在的最佳实践。
顺便说一句,我正在为ASP.NET Core 3.1进行开发。
推荐答案
在V12中,检查容器是否存在有两种方式。
1。
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
//get a BlobContainerClient
var container = blobServiceClient.GetBlobContainerClient("the container name");
//you can check if the container exists or not, then determine to create it or not
bool isExist = container.Exists();
if (!isExist)
{
container.Create();
}
//or you can directly use this method to create a container if it does not exist.
container.CreateIfNotExists();
您可以直接创建一个BlobContainerClient
,然后使用下面的代码:
//create a BlobContainerClient
BlobContainerClient blobContainer = new BlobContainerClient("storage connection string", "the container name");
//use code below to check if the container exists, then determine to create it or not
bool isExists = blobContainer.Exists();
if (!isExist)
{
blobContainer .Create();
}
//or use this code to create the container directly, if it does not exist.
blobContainer.CreateIfNotExists();
这篇关于如何检查Azure Blob Storage V12中是否存在容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文