Recently we’ve been replacing many storage solutions (like FTP) with Azure Blob Storage because it is very easy to programmatically implement in applications and it is very easy to maintain.
This tutorial assumes you know how to create an Azure Blob Storage in your Azure account.
Using the Azure Blob Storage exists of the following steps:
Install the required NuGet packages
Install the “Azure.Storage.Blobs“ package. The latest version is 12.8.0 at the time of writing, so that’s what I used.
Creating a Blob reader/writer service
We will be creating a service to interact (read and write) with our Blob because we may want to use this service through the application. Just FYI, a Blob can consist of multiple “BlobContainers”. Perhaps you want one container for profile images, one for documents and one for public content. This service has support for multiple containers, handy right? 😉
public interface IBlobService
{
Task UploadFileBlobAsync(byte[] content, string fileName, BlobContainer container);
Task<byte[]> GetFileBlobAsync(string fileName, BlobContainer container);
Task<bool> DoesBlobExists(string fileName, BlobContainer container);
Task DeleteFromBlob(string fileName, BlobContainer container);
}
public class BlobService : IBlobService
{
private readonly BlobServiceClient _blobServiceClient;
public BlobService(BlobServiceClient blobServiceClient)
{
_blobServiceClient = blobServiceClient;
}
public async Task<bool> DoesBlobExists(string fileName, BlobContainer container)
{
var containerClient = GetContainerClient(container);
var blobClient = containerClient.GetBlobClient(fileName);
return await blobClient.ExistsAsync();
}
public async Task UploadFileBlobAsync(byte[] content, string fileName, BlobContainer container)
{
using (Stream stream = new MemoryStream(content))
{
var containerClient = GetContainerClient(container);
var blobClient = containerClient.GetBlobClient(fileName);
await blobClient.UploadAsync(stream);
}
}
public async Task<byte[]> GetFileBlobAsync(string fileName, BlobContainer container)
{
var containerClient = GetContainerClient(container);
var blobClient = containerClient.GetBlobClient(fileName);
if (await blobClient.ExistsAsync())
{
var response = await blobClient.DownloadAsync();
using (MemoryStream stream = new MemoryStream())
{
response.Value.Content.CopyTo(stream);
return stream.ToArray();
}
}
else
{
return null;
}
}
private BlobContainerClient GetContainerClient(BlobContainer container)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(container.ToString());
containerClient.CreateIfNotExists(PublicAccessType.Blob);
return containerClient;
}
public async Task DeleteFromBlob(string fileName, BlobContainer container)
{
var containerClient = GetContainerClient(container);
var blobClient = containerClient.GetBlobClient(fileName);
if (await blobClient.ExistsAsync())
{
await blobClient.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);
}
}
}
public enum BlobContainer
{
images,
documents,
publiccontent
}
Now, we just need to add our blobstorage connection to the Appsettings file so that we can register it globally..
You can find your Azure Blob connection string in your Azure accounts. Search for your Blob storage name and copy one of the two available keys:
{
......
"Azure": {
"BlobStorage":
"DefaultEndpointsProtocol=https;AccountName=xxxxxxxxx;AccountKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/nTKQ==;EndpointSuffix=core.windows.net"
},
....
}
Register the BlobService
Register the BlobService in your Startup.cs like this:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
......
services.AddScoped(x => new BlobServiceClient(Configuration["Azure:BlobStorage"]));
services.AddScoped<IBlobService, BlobService>();
}
That’s it! You can now dependency inject the service anywhere you like. Write to the blobstorage and read from the storage!