Create Azure Blob Storage Account using C# .NET
Today in this article, we shall see Azure Blob Storage C# .NET with an example.
We will use Blob storage Client library v12 for .NET and perform create, and delete of Blob storage.
Azure Blob storage is used for storing the data in the cloud. Blob storage is optimized for storing unstructured data supporting multiple formats of files such as text or binary data.
Today in this article, we will cover below aspects,
Let’s see step by step all the above operation and understand how it works.
Prerequisites
- Create Azure subscription
- Create Storage Account in Azure Portal
Getting Started
Create any .NET application. Here I am using the Console .NET Core application,
Please install Nuget packages,
PM> Install-Package Azure.Storage.Blobs -Version 12.6.0
Or Using Nuget Package Manager
To start with you shall be needing the StorageAccount Connection string from the Azure portal.
Please follow the below steps to get the connection string from the Azure portal,
- Sign in to the Azure portal account
- Go to your storage account. Ex. vstorageaccount
- Click on the Access keys.
- Copy the Connection string value under key1
- Either add the Connection string to your apsettings.json or add it to the Environment variable.
Below is how I can get the Connection string from the Azure portal,
Create the Azure Blob Storage Container – Azure .NET
You can create a storage container using the BlobContainerClient object and calling Create or CreteAsync method as shown below,
The operation creates a new container under the given account.
If the container with the same name already exists, the operation fails.
string connectionString = configuration["BlobConnectionString"];
// Get a reference to a container
BlobContainerClient container = new BlobContainerClient(connectionString,
"pdffilecontainer");
await container.CreateAsync();
The above code programmatically will generate the new container name called “pdffilecontainer“.
Once created successfully, you can see the newly generated container in the Azure portal as below,
Upload and Download – Data to Azure Blob Storage Container
Please visit the below article on how to upload or download files into storage containers.
Delete Azure Storage Container
Deleting the Storage Container can be done using sync and async methods available using the BlobContainerClient class object.
Deleting Blobs from Storage Container
Deleting Blobs can be done using the BlobContainerClient class object as below,
// Get a reference to a container
BlobContainerClient container = new
BlobContainerClient(connectionString, containerName);
//Delete Blob
container.DeleteBlob(blobName);
One can also easily enumerate all the Blobs available in a Container and perform the specific operation as below,
Using Batching for Multiple Requests
One can get fine-grained control of operations by using the Azure Storage Blobs Batch client library for .NET. Using this library one can batch multiple Azure Blob Storage operations in a single request.
References:-
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Summary
Today in this article we learned how to Create or Delete Storage Container or Delete the blobs from a container in the Azure cloud using Blob storage client library v12 for .NET-based applications.
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.