Overview

Azure Storage is cloud service that provides storage which is fully maintained by Microsoft and is highly scalable, available and robust. Azure storage comprises mainly of Blob, File, Table and Queue storage, however, the following article discusses only block blobs.

Pre-Requisites

Microsoft Azure Storage Account

Make sure you have created a Storage Account on Azure portal, we will be needing the Access Keys to access the Storage Account. The same can also be done locally using an Azure Storage Emulator if you don’t have an Azure Subscription.

Create Azure Storage Account
Create Azure Storage Account

You can find both access keys and connection strings under the Access Keys section. I would suggest copying one of the connection strings and using it in your application.

Connection String Azure Storage Account
Connection String Azure Storage Account

Install Microsoft.WindowsAzure.ConfigurationManager NuGet Package

Make sure you have added the Microsoft.WindowsAzure.ConfigurationManager NuGet Package to your project. To add the pacakge Go to Tools > Nuget Package Manager > Package Manager Console

Install-Package Microsoft.WindowsAzure.ConfigurationManager
Install Microsoft.WindowsAzure.ConfigurationManager
Install Microsoft.WindowsAzure.ConfigurationManager

Storage Account Architecture

This is what the high-level view of the Storage Account looks like. Storage Account is the first point of access. Each storage account contains various containers, and each container is further comprised of blobs and each blob can either be an append, block or page blobs.

Storage Account Architechture
Storage Account Architechture

Create Container

To create a container we first need to get the reference to the storage account, using the storage account reference we then create a blob client which is a client-side representation of blob storage. An important thing to note is that the name of the container must always be lowercase as per container naming rules.

// Get Reference to the storage account using the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_strStorageConnection);
// Create a client side representation of Azure Blob storage
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Get Container reference
CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
// We can also use CreateIfNotExistsAsync method, however it doesn't convey the information if the container
// already exists. CreateAsync on the other hand throws an exception if the container already exists.
await blobContainer.CreateAsync();

Upload Blob

We upload the blob from a file using the container reference created previously via the UploadFromFileAsync method of the CloudBlockBlob class, the blob gets overwritten by the uploaded blob if a blob with the same name already exists in the container.

// Get Blob reference using the container reference previously created
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
// Upload BlockBlob
await blockBlob.UploadFromFileAsync(blobPath);

Download Blob

We download the blob to a file using the container reference created previously via the DownloadToFileAsync method of the CloudBlockBlob class, the blob gets overwritten by the downloaded blob if a blob with the same name already exists.

// Get Blob reference using the container reference previously created
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
// Download Blob
await blockBlob.DownloadToFileAsync(blobPath,FileMode.Create);

Delete Blob

Delete blob operation is also performed using the container reference created previously using the DeleteAsync method of the CloudBlockBlob class.

// Get Blob reference using the container reference previously created
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
// Delete Blob
await blockBlob.DeleteAsync();

Copy Blob

In order to copy the blobs, we need reference for both the source and destination container. The same can be obtained using the GetContainerReference method. If you want to copy the blob within the same container, the reference for source and destination container will be same. The copy operation takes place using the StartCopyAsync method of CloudBlockBlob class

// Get the source container reference to copy the blob from
CloudBlobContainer fromCloudBlobContainer = CreateContainer(blobClient, fromContainerName).GetAwaiter().GetResult();
// Get the source blob reference
CloudBlockBlob fromBlockBlob = fromCloudBlobContainer.GetBlockBlobReference(fromBlobName);

// Get the destination container reference to copy the blob to
CloudBlobContainer toCloudBlobContainer = CreateContainer(blobClient, toContainerName).GetAwaiter().GetResult();
// Get the destination blob reference
CloudBlockBlob toBlockBlob = toCloudBlobContainer.GetBlockBlobReference(toBlobName);

// Copy the blob
await toBlockBlob.StartCopyAsync(new Uri(fromBlockBlob.Uri.AbsoluteUri));

List Blobs

To list all the blobs in a container, we call the ListBlobsSegmentedAsync method of CloudBlobContainer class. The result is returned as a result segment containing a collection of blob items. We call the ListBlobsSegmentedAsync method until the BlobContinuationToken is null. The continuation token is returned as null when there are no more blobs to be returned.

 // Get Container reference
CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
List<IListBlobItem> lstBlobs = new List<IListBlobItem>();
BlobContinuationToken continuationToken = null;

// Get the list of blobs in the container and store them into a list
BlobResultSegment resultSegment = await blobContainer.ListBlobsSegmentedAsync(continuationToken);
// Update continuation token
continuationToken = resultSegment.ContinuationToken;
// Add Blobs to the list
lstBlobs.AddRange(resultSegment.Results);

Source Code

You can find the source code at Github AzureStorageBlobs repository as .Net Core solution.

Final note

There are many other operations that can be performed on azure storage blobs, which are not covered in this post, I would encourage you to explore more about them. If you have any questions or concerns, feel free to leave a comment.