Reputation: 2551
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.
Upvotes: 29
Views: 38326
Reputation: 29940
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();
Upvotes: 53
Reputation: 335
I have the following method to get a SAS token, everything works fine.
private Uri GetUriSasToken()
{
string storedPolicyName = null;
string connectionString = _config.GetConnectionString("BlobConnection");
BlobContainerClient containerClient = new BlobContainerClient(connectionString, "stock");
// Check whether this BlobContainerClient object has been authorized with Shared Key.
if (containerClient.CanGenerateSasUri)
{
// Create a SAS token that's valid for one hour.
BlobSasBuilder sasBuilder = new BlobSasBuilder()
{
BlobContainerName = containerClient.Name,
Resource = "c"
};
if (storedPolicyName == null)
{
sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
sasBuilder.SetPermissions(BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List);
}
else
{
sasBuilder.Identifier = storedPolicyName;
}
Uri sasUri = containerClient.GenerateSasUri(sasBuilder);
return sasUri;
}
else
{
_logger.LogError(@"BlobContainerClient must be authorized with Shared Key credentials to create a service SAS.");
return null;
}
}
Then, I call the above method with the following code:
BlobServiceClient blobServiceClient = new BlobServiceClient(GetUriSasToken(), null);
var blobName = _config.GetValue<string>("BlobName");
var containerName = _config.GetValue<string>("ContainerName");
var fileName = _config.GetValue<string>("FileName");
var containerClient = blobServiceClient.GetBlobContainerClient(containerName);
Is there any way I can verify that the container exists?
I'm not sure I can do:
containerClient.Exists
I used it but it returns an error that the Blob does not exists, but I want to check first if the container exists.
Anyone has done this?
Upvotes: -1
Reputation: 3843
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
Upvotes: 8
Reputation: 136126
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();
Upvotes: 2