Store and retrieve files from Azure blob storage

ASP.NET Core web applications often need to store files on the server. One of the approaches to storing files on the server side is to store them into Azure blob storage. For example, you can store documents, audio files, video files, and images into Azure blob storage. This article shows you how to add, download, and remove files from Azure blob storage.

In order to work with this example you need to create an Azure storage account. You can do that by signing in to the Azure portal and clicking the Storage account option at the top.

Then complete the form that follows and create the account.

The storage account creation might take some time. Once you get the success notification, go to the storage account you just created and click on the Access Keys option.

Then copy the values under key1 section (key and connection string) and keep them at a handy location. You will need them in your .NET Core application.

For the sake of simplicity I am going to use a console application (.NET Core) to demonstrate the use of Azure blob storage but you can use any other kind of projects also.

Once the project is created add NuGet package for Microsoft.Azure.Storage.Blob.

Then write the following code to upload an image file from your local folder to Azure blob storage.

static void Main(string[] args)
{
    string connStr = "your_connection_string_here";

    CloudStorageAccount account = CloudStorageAccount.Parse(connStr);

    CloudBlobClient client = account.CreateCloudBlobClient();

    CloudBlobContainer container = client.
GetContainerReference("myblobcontainer");

    container.CreateIfNotExists();

    CloudBlockBlob blob = container.
GetBlockBlobReference("myblob");


    blob.UploadFromFile(@"C:\Test\Image1.jpg");

}

As you can see the code stores the Azure storage account's connection string in a string variable. You should replace it with your own connection string.

The code then creates an object of CloudStorageAccount class. The CloudStorageAccount class represents an Azure storage account and you supply the connection string to it while creating the object.

Then an object of CloudBlobClient class is created. The CloudBlobClient class allows you to store and retrieve blob data to your account.

The code then uses the  GetContainerReference() method to get a reference of a blob container (CloudBlobContainer object). At this stage no such container exists and hence the code also calls the CreateIfNotExists() method of the CloudBlobContainer class.

It then creates a CloudBlockBlob object by specifying the name of the blob. If this name doesn't exist it will be created and its reference is returned otherwise an existing reference is returned.

Finally, the code calls the UploadFromFile() method of CloudBlockBlob class to upload a file from local path to the blob.

The following figure shows the myblob entry getting created in the Azure storage account.

Clicking on the myblobcontainer will take you to its content and myblob entry will be visible.

How would you download the content of myblob into a file? That's easy. Most of the code remains the same except the last line. Instead of UploadFromFile() you would use:

blob.DownloadToFile(@"C:\Test\Image2.jpg",FileMode.CreateNew);

The DownloadToFile() accepts a file name where the blob content is to be stored.

If you wish to delete a blob entry, you can do so using Delete() or DeleteIfExist() methods of CloudBlockBlob class.

That's it for now! Keep coding!!


Bipin Joshi is a software consultant, trainer, author, and yoga mentor having 24+ years of experience in software development, consulting, and training. He conducts instructor-led online training courses in ASP.NET Core, ASP.NET MVC, and Design Patterns for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get article updates : Facebook  Twitter  LinkedIn

Posted On : 06 May 2019


Tags : ASP.NET ASP.NET Core .NET Framework C# Visual Studio


Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.

  

Receive Weekly Updates