Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you check that a container exists in Azure Blob Storage V12

Previously when using Azure Blob Storage SDK V11, if you wanted to Create a container but were unsure if the container existed you could use CreateIfNotExists.

However in version V12, CreateIfNotExists is no longer available and the only example I can find from Microsoft is to simply create a Container without checking if it already exists.

So, does anyone know the best practice in V12 to check if a container exists before trying to create it.

Incidentally, I'm developing for ASP.Net Core 3.1.

like image 251
Mitch Avatar asked Apr 09 '20 19:04

Mitch


People also ask

Where is the container name in Azure Blob Storage?

You could access them via your storage account in the azure portal, refer to the screenshot. Choose a container, you could see the blobs, including names, blob type, size,etc.

What is container in Azure Blob Storage?

Azure Blob storage is a feature of Microsoft Azure. It allows users to store large amounts of unstructured data on Microsoft's data storage platform. In this case, Blob stands for Binary Large Object, which includes objects such as images and multimedia files.


Video Answer


3 Answers

In v12, there are 2 ways to check if the container exists or not.

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

You can directly create a BlobContainerClient, then use code below:

//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();
like image 74
Ivan Yang Avatar answered Oct 12 '22 18:10

Ivan Yang


The accepted answer is fine. But I usually use the async version of it.

var _blobServiceClient = new BlobServiceClient(YOURCONNECTIONSTRING);
var containerClient = _blobServiceClient.GetBlobContainerClient(YOURCONTAINERNAME);
await containerClient.CreateIfNotExistsAsync(Azure.Storage.Blobs.Models.PublicAccessType.BlobContainer);

The version I'm using is Azure.Storage.Blobs v12.4.1

like image 39
Mehmet Taha Meral Avatar answered Oct 12 '22 18:10

Mehmet Taha Meral


However in version V12, CreateIfNotExists is no longer available and the only example I can find from Microsoft is to simply create a Container without checking if it already exists.

I am not sure why do you say CreateIfNotExists is no longer available in version 12 of storage client library. It is certainly there in BlobContainerClient class. Here's the direct link: CreateIfNotExists.

    var connectionString = "UseDevelopmentStorage=true";            
    var containerClient = new BlobContainerClient(connectionString, containerName);
    containerClient.CreateIfNotExists();
like image 2
Gaurav Mantri Avatar answered Oct 12 '22 20:10

Gaurav Mantri