Joydip Kanjilal
Contributor

How to use Azure Blob storage in ASP.Net Core

how-to
Jul 23, 20184 mins
Cloud ComputingMicrosoft AzureSmall and Medium Business

Take advantage of Azure Blob storage to store data in the Microsoft cloud from your ASP.Net Core applications

It’s easy to take advantage of Microsoft Azure cloud resources in ASP.Net Core, Microsoft’s cross-platform, lean, and modular framework for building high-performance web applications. You can use an Azure storage account to store or retrieve data, for example. Such data might include files, blobs, queues, or tables. In this article we’ll look at how we can upload data to Azure Blob storage from an ASP.Net Core application.

Create an ASP.Net Core Web API project in Visual Studio

Assuming that you’re running Visual Studio 2017, you can follow the steps outlined below to create an ASP.Net Web API project in Visual Studio 2017.

  1. In the Visual Studio IDE, click on File > New > Project.
  2. Select “ASP.Net Core Web Application (.Net Core)” from the list of templates displayed.
  3. Specify a name for the project.
  4. Click OK to save the project.
  5. Select “API” in the “New .Net Core Web Application…” window.
  6. Uncheck the “Enable Docker Support” checkbox.
  7. Select “No Authentication” as we won’t be using authentication here.
  8. Click OK.

This will create a new ASP.Net Core 2.1 Project in Visual Studio 2017.

As a prerequisite, you should have an Azure storage account. To learn how to create an Azure storage account, you can refer to this article.

Implement the UploadDataController service in ASP.Net Core

In this section we’ll explore how we can implement a simple ASP.Net Core controller to upload data to Azure Blob storage. To create the controller, right-click on the Controllers folder in the Solution Explorer Window, select Add > Controller, choose “API Controller – Empty,” and click Add. Next, name the controller “UploadDataController” and click Add again.

Now add the WindowsAzure.Storage package via the NuGet Package Manager. Alternatively, you can use the NuGet Package Manager Console to install the package with the following command:

Install-Package WindowsAzure.Storage -ProjectName YourProjectNameHere

Connect to your Azure Blob storage container

Assuming that you have successfully installed the WindowsAzure.Storage package in your project, you can now start writing the code. You will need the connection string from your Azure storage account. Then you will need to create a reference to the Azure Blob client as shown in the code snippet given below. The Azure Blob client, named CloudBobClient below, refers to the Blob storage in your Azure storage account.

string connectionString ="Specify the connection string to connect to the Azure blob storage here.";
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

Once you have the reference to the CloudBlobClient instance, you can create an instance of the CloudBlobContainer, the actual container object in your Azure Blob storage. Note that the name of the container is passed as a parameter to the GetContainerReference method of the CloudBlobClient instance.

CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);

Next, you can retrieve a reference to the block using the following code. Block blobs—one of three kinds of blobs in Azure Blob storage—make it easy to upload large blobs (up to 4.75TB).

CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);

The complete UploadData controller method in ASP.Net Core

Finally, here is the complete source code of the controller method named UploadData.

public async Task UploadData(Stream stream, string filename)
        {
            try
            {
                string connectionString ="Specify the connection string to connect to the Azure blob storage here.";
                string containerName ="Specify the container name here.";
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
                CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);
                await cloudBlockBlob.UploadFromStreamAsync(stream);
                stream.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }

You can leverage Azure Blob storage to store large amounts of unstructured data in Azure—documents, images, audio and video files, log files, backups, anything. You can access this data from anywhere, and take advantage of role-based access control and encryption to secure the data as well.

In this article we examined how we can upload data to Azure Blob Storage using a RESTful ASP.Net Core service. I’ll discuss more ways we can work with Azure Blob storage in future posts here.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author