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

Kulvinder Singh February 12, 2019

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

This article is for those who are trying to get a hands-on experience of Azure. Today, we can see media files everywhere, such as images, videos, audio, etc. In order to store them on the cloud securely, we can use Azure Blob Storage.

You need an Azur account for the purpose. If you have one, you may log in here and if you don’t have an Azure account, you can sign up for a 30-day free trial.

Once logged in, go to the dashboard and click on “Storage Account”. After that Add Storage Account in Azure Portal.

Now, our Azure part is done. We will go to our Visual Studio and click on “New Project” and select “Web Application in ASP.NET Core”.
Once our project is created, we need to add some Azure packages from NuGet in order to contact with Azure(WindowsAzure.Storage).
Add a class to encapsulate settings as follows:
public class AzureBlobSetings

 

{ public AzureBlobSetings(string storageAccount,string storageKey,string containerName) {

 

if (string.IsNullOrEmpty(storageKey)) throw new ArgumentNullException(“StorageKey”);public class AzureBlobSetings { public AzureBlobSetings(string storageAccount, string storageKey, string containerName) { if (string.IsNullOrEmpty(storageAccount)) throw new ArgumentNullException(“StorageAccount”);

 

if (string.IsNullOrEmpty(containerName)) throw new ArgumentNullException(“ContainerName”);

 

this.StorageAccount = storageAccount;

 

this.StorageKey = storageKey;

 

this.ContainerName = containerName; } public string StorageAccount { get; } public string StorageKey { get; } public string ContainerName { get; } }

 

 

Add a class to encapsulate a blob item as follows:

 

public class AzureBlobItem { public AzureBlobItem(IListBlobItem item) {

 

this.Item = item;

 

} public IListBlobItem Item { get; } public bool IsBlockBlob => Item.GetType() == typeof(CloudBlockBlob); public bool IsPageBlob => Item.GetType() == typeof(CloudPageBlob); public bool IsDirectory => Item.GetType() == typeof(CloudBlobDirectory); public string BlobName => IsBlockBlob ? ((CloudBlockBlob)Item).Name : IsPageBlob ? ((CloudPageBlob)Item).Name : IsDirectory ? ((CloudBlobDirectory)Item).Prefix : “”; public string Folder => BlobName.Contains(“/”) ? BlobName.Substring(0, BlobName.LastIndexOf(“/”)) : “”; public string Name => BlobName.Contains(“/”) ? BlobName.Substring(BlobName.LastIndexOf(“/”) + 1) : BlobName; }

 

 

Add a class to encapsulate storage access and also add a private helper method to access the storage as mentioned below:
private async Task<CloudBlobContainer> GetContainerAsync()

 

{

 

//Account

 

CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(settings.StorageAccount, settings.StorageKey), false);

 

//Client

 

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

 

//Container

 

CloudBlobContainer blobContainer =blobClient.GetContainerReference(settings.ContainerName);

 

await blobContainer.CreateIfNotExistsAsync();

 

return blobContainer;

 

}

 

private async Task<CloudBlockBlob> GetBlockBlobAsync(string blobName) { //Container CloudBlobContainer blobContainer = await GetContainerAsync(); //Blob CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName); return blockBlob; } private async Task<List<AzureBlobItem>> GetBlobListAsync(bool useFlatListing = true) { //Container CloudBlobContainer blobContainer = await GetContainerAsync(); //List var list = new List<AzureBlobItem>(); BlobContinuationToken token = null; do { BlobResultSegment resultSegment = await blobContainer.ListBlobsSegmentedAsync(“”, useFlatListing,new BlobListingDetails(), null, token, null, null); token = resultSegment.ContinuationToken; foreach (IListBlobItem item in resultSegment.Results) { list.Add(new AzureBlobItem(item)); } } while (token != null); return list.OrderBy(i => i.Folder).ThenBy(i => i.Name).ToList(); }

 

Now add public methods to upload and download blob items as follows:

public async Task UploadAsync(string blobName, string filePath) { //Blob CloudBlockBlob blockBlob = await GetBlockBlobAsync(blobName);

 

Add methods to get a list of blob items as follows:

 

public async Task<List<AzureBlobItem>> ListAsync()

 

 


Inject and use storage helper as follows:
public class HomeController: Controller

 

private readonly IAzureBlobStorage blobStorage;

 

public HomeController(IAzureBlobStorage blobStorage)

 

{

 

this.blobStorage = blobStorage;

 

}

 

}

 

Lets’s Talk

About your ideas and concept