Introduction
Microsoft has introduced a new library, Azure.Storage.Blobs for handling the Blob operations. I have already written an article about Blob operations a few years ago on C# Corner.
Above article discussed based on an application which was built with .NET Core 2.2 and Angular 8. That application was referring to the library WindowsAzure.Storage which was now marked as obsolete by Microsoft.
Since both .NET Core and Angular versions are changed regularly and recently came with the latest versions 6 and 14 respectively with drastic changes, I have decided to create a new article which will use .NET 6 and Angular 16. I will explain all the steps one by one.
We can create an Azure storage account first.
Create Azure storage account on Azure Portal
We can log into Azure portal and create a new Azure Blob storage.

We can choose any existing resource group or create a new resource group. We must choose a region and supply a valid account name.
We can click the Create button to create our new Blob storage. The new resource will be deployed within a few moments.
Currently, there are four types of services available in Blob storage.

Blob service, File service, Queue service and Table service are the services. In this article, we will be discussing about Blob service.
Create ASP.NET Core 6.0 Web API using Visual Studio 2022
We can create our Web API project using Visual Studio 2022. We must choose the ASP.NET Core template and .NET 6.0 framework.

Our new project will be created with default Swagger documentation soon.
We can install the Azure.Storage.Blobs library using NuGet package manager.

We can create a configuration inside the appsettings.json file and add our Azure Storage account connection string and container name.
You can get the storage connection from the Access keys section of storage account. Click the Show keys button to get the connection string.

Please note that, we have not yet created a Container inside the Blob storage. Our application will automatically create a container if it does not exist. We will write the code for that later.
We can put the connection string and container name inside the appsettings.json file.
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"MyConfig": {
"StorageConnection": "DefaultEndpointsProtocol=https;AccountName=sarathblobstorage;AccountKey=IMGCgU7hTXUO9hAzRd9YZXCfGy/QWFX048+301cNpxVOGkoRV+9+Lad/tJ5XjDfR2pUALEXbSk2J+AStloNliQ==;EndpointSuffix=core.windows.net",
"ContainerName": "sarathcontainer"
}
}
We need one interface and service class to provide CRUD operations. This service class can be used from our API controller.
We can create our IBlobStorage interface with the method definitions below.
IBlobStorage.cs
namespace AzureBlobDotNET6.Helpers
{
public interface IBlobStorage
{
public Task<List<string>> GetAllDocuments(string connectionString, string containerName);
Task UploadDocument(string connectionString, string containerName, string fileName, Stream fileContent);
Task<Stream> GetDocument(string connectionString, string containerName, string fileName);
Task<bool> DeleteDocument(string connectionString, string containerName, string fileName);
}
}
We can create BlobStorage service class and implement above IBlobStorage interface.
BlobStorage.cs
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
namespace AzureBlobDotNET6.Helpers
{
public class BlobStorage : IBlobStorage
{
public async Task<List<string>> GetAllDocuments(string connectionString, string containerName)
{
var container = BlobExtensions.GetContainer(connectionString, containerName);
if (!await container.ExistsAsync())
{
return new List<string>();
}
List<string> blobs = new();
await foreach (BlobItem blobItem in container.GetBlobsAsync())
{
blobs.Add(blobItem.Name);
}
return blobs;
}
public async Task UploadDocument(string connectionString, string containerName, string fileName, Stream fileContent)
{
var container = BlobExtensions.GetContainer(connectionString, containerName);
if (!await container.ExistsAsync())
{
BlobServiceClient blobServiceClient = new(connectionString);
await blobServiceClient.CreateBlobContainerAsync(containerName);
container = blobServiceClient.GetBlobContainerClient(containerName);
}
var bobclient = container.GetBlobClient(fileName);
if (!bobclient.Exists())
{
fileContent.Position = 0;
await container.UploadBlobAsync(fileName, fileContent);
}
else
{
fileContent.Position = 0;
await bobclient.UploadAsync(fileContent, overwrite: true);
}
}
public async Task<Stream> GetDocument(string connectionString, string containerName, string fileName)
{
var container = BlobExtensions.GetContainer(connectionString, containerName);
if (await container.ExistsAsync())
{
var blobClient = container.GetBlobClient(fileName);
if (blobClient.Exists())
{
var content = await blobClient.DownloadStreamingAsync();
return content.Value.Content;
}
else
{
throw new FileNotFoundException();
}
}
else
{
throw new FileNotFoundException();
}
}
public async Task<bool> DeleteDocument(string connectionString, string containerName, string fileName)
{
var container = BlobExtensions.GetContainer(connectionString, containerName);
if (!await container.ExistsAsync())
{
return false;
}
var blobClient = container.GetBlobClient(fileName);
if (await blobClient.ExistsAsync())
{
await blobClient.DeleteIfExistsAsync();
return true;
}
else
{
return false;
}
}
}
public static class BlobExtensions
{
public static BlobContainerClient GetContainer(string connectionString, string containerName)
{
BlobServiceClient blobServiceClient = new(connectionString);
return blobServiceClient.GetBlobContainerClient(containerName);
}
}
}
We need a minor change in the Program.cs file to enable CORS (Cross Origin Resource Sharing). So that we can send Http requests from Angular application without any issues. We can also inject our BlobStorage service class and interface using dependency injection.
Program.cs
using AzureBlobDotNET6.Helpers;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IBlobStorage, BlobStorage>();
var MyAllowedOrigins = "_myAllowedOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(MyAllowedOrigins,
builder =>
{
builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.UseCors(MyAllowedOrigins);
app.Run();




Join the conversation! Your thoughts help the community grow.