Azure Blob Storage File Management API
What is Azure Blob Storage?
Azure Blob Storage is a Microsoft Cloud Storage. Azure Blob Storage is used to manage the files for storing or reading.
Storage Account
A Storage account gives us a unique namespace. Every file we save or store on Azure Storage must have an address, and that address is nothing but the unique name of our Storage Account name. The combination of the storage account name and Azure blob Storage endpoint forms the base address. For example, if our storage account is named as 'my-azure-account' then the base address will be like 'https://resourceName.blob.core.window.net'.
Container
Those containers are just folders that obviously contains certain files.
Blob
This is the file stored on the container
Resource Creation
First, we have to 'Sing-Up' in 'Microsoft Azure Portal', after completing 'Sing-Up' and then 'Sing-In' portal we can observe an azure portal like below. And then click on the 'Create A Resource' button.
Search like 'Storage Account' and then click on the 'Create' button.
Fill out the 'Create A Storage Account' form.
- Here in the 'Subscription' field make sure to select your subscription which may be paid or free. If you use paid subscription also the Microsoft won't charge for basic usage so it will be a benefit for those who are newly working with Azure Blob Storage.
- Here the 'Resource Group' is like a folder which holds the azure services in our case Azure Blob Storage Service going to be stored in the defined 'Resource Group' name
- Here 'Storage Account Name' field, here give a meaning full name because this name will be used to generate the subdomain URL for our Azure Blob Storage.
- Here 'Region' you can leave the default or you choose the nearest location to host your server
Now Click on the 'Review. button at the bottom, so that we can directly navigate to the 'Review' tab because all the remaining tabs can be skipped with default values. Now on the 'Review' tab click on the 'Create' button at the bottom
Now click on the 'Go to resource' button
Now in our Azure Blob Storage account, go to the 'Container' left side menu item, then click on '+ Container' button. Then in the create container form give your container name and in the 'Public Access Level' drops down give a value like 'Container(anonymous read access for containers)'. Finally, click on the create button.
We can observe our newly created container below.
Web Api Creation
Let's create the .NET 7 Web API application to implement our demo on Azure Blob Storage. The most recommended IDEs are Visual Studio 2022(V17.4.* supports .NET 7) or Visual Studio Code. In this demo, I'm using the Visual Studio Code editor.
GITCODE
Install Azure Blob Storage Package
Let's install the Azure Blob Storage package.
GITCODE
Add Azure Storage ConnectionString
Go to our Azure Storage Account, then go to the 'Security + Network' menu on the left-hand side menu, then click on the 'Access Keys' then we can see 'Key1' & 'Key2' connection strings any one can be used into our API.
In the 'appsettings.Development.json' let's add the connection string.
GITCODE
Register BlobServiceClient
In the 'Program.cs' file register the 'Azure.Storage.Blob.BlobServiceClient'.
GITCODE
Here for the 'BlobServiceClient' instance pass our connection string value as an input parameter.
Create an Interface for File Service
To implement our core logic let's create the service files like 'IFileService.cs' & 'FileService.cs' in the 'Services' folder(new folder).
Interface:
GITCODE
Class:
GITCODE
Now register our service file into the Program.cs
GITCODE
Create an API Controller
Let's create a new API controller like FileController
GITCODE
Create the File Service Model
GITCODE
Let's add the logic for uploading the file to our Azure Blob Storage in our FileService.
Interface:
GITCODE
Class:
GITCODE
Now let's add the file upload endpoint in our controller.
GITCODE
Add File Read Endpoint
Let's implement the logic for reading the file from Azure Blob Storage.
GITCODE
GITCODE
Let's implement the endpoint to read the file.
GITCODE
Add File Download Endpoint
Let's add the file download endpoint as below.
GITCODE
Here download endpoint is almost similar to our read endpoint only change we used the overloaded File method that takes the name of the file to download.
Wrapping Up !!!
Hopefully, I think this article delivered some useful information of approaches to consuming Azure Blob Storage in .Net7 Web API. I love to have your feedback, suggestions, and better techniques in the comment section below.