Azure Blob Storage V12 にコンテナーが存在するかどうかを確認するにはどうすればよいですか? 質問する

Azure Blob Storage V12 にコンテナーが存在するかどうかを確認するにはどうすればよいですか? 質問する

以前、Azure Blob Storage SDK V11 を使用しているときに、コンテナーを作成する必要があるが、コンテナーが存在するかどうか不明な場合は、CreateIfNotExists を使用できました。

ただし、バージョン V12 では、CreateIfNotExists は使用できなくなり、Microsoft で見つけることができる唯一の例は、コンテナーが既に存在するかどうかを確認せずに単にコンテナーを作成することです。

それで、コンテナを作成する前に、コンテナが存在するかどうかを確認するための V12 のベストプラクティスを知っている人はいますか。

ちなみに、私は ASP.Net Core 3.1 向けに開発しています。

ベストアンサー1

v12 では、コンテナが存在するかどうかを確認する方法が 2 つあります。

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();

おすすめ記事