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.
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.
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.
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();
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With