Working with Azure Storage Blobs using .Net Core
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.
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.
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
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.
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.
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.
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.
Delete Blob
Delete blob operation is also performed using the container reference created previously using the DeleteAsync
method of the CloudBlockBlob
class.
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
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.
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.