How to use Azure Blob storage in ASP.Net Core

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

How to use Azure Blob storage in ASP.Net Core
Lukas Budimaier (CC0)

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<bool> UploadData(Stream stream, string filename)
                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);
                return true;
                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.

Copyright © 2018 IDG Communications, Inc.