Azure Blob Storage In ASP.NET Core 2.0

Problem

How to use Azure Blob storage in ASP.NET Core.

Solution

Create a class library and add NuGet package - WindowsAzure.Storage

Add a class to encapsulate settings,

  1. public class AzureBlobSetings  
  2.  {  
  3.      public AzureBlobSetings(string storageAccount,  
  4.                                     string storageKey,  
  5.                                     string containerName)  
  6.      {  
  7.          if (string.IsNullOrEmpty(storageAccount))  
  8.              throw new ArgumentNullException("StorageAccount");  
  9.   
  10.          if (string.IsNullOrEmpty(storageKey))  
  11.              throw new ArgumentNullException("StorageKey");  
  12.   
  13.          if (string.IsNullOrEmpty(containerName))  
  14.              throw new ArgumentNullException("ContainerName");  
  15.   
  16.          this.StorageAccount = storageAccount;  
  17.          this.StorageKey = storageKey;  
  18.          this.ContainerName = containerName;  
  19.      }  
  20.   
  21.      public string StorageAccount { get; }  
  22.      public string StorageKey { get; }  
  23.      public string ContainerName { get; }  
  24.  }  

Add a class to encapsulate a blob item,

Now add public methods to upload and download blob items,

Add methods to get a list of blob items,

Inject and use storage helper,

Note

Sample code has a controller with actions for listing, downloading and uploading items.

In ASP.NET Core Web Application, configure services,

Discussion

The sample code will require you to set up an Azure account, blob storage account, and container. Instructions for these can be found  here.

Source Code

GitHub