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:

Add a class to encapsulate a blob item:

Add a class to encapsulate storage access. Add a private helper methods to access storage:

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 setup Azure account, blob storage account and container. Instructions for these could be found at: https://docs.microsoft.com/en-gb/azure/storage/storage-create-storage-account#create-a-storage-account

Source Code

GitHub: https://github.com/TahirNaushad/Fiver.Azure.Blob

Leave a Reply