Introduction
In this blob post series, I will showcase how to connect to Azure Storage Account containers to read and upload data.
“Azure Blob Storage, Microsoft’s cloud-based object storage solution, is optimized for vast amounts of unstructured data like text or binary files. It is designed for use cases such as serving images or documents to browsers, storing files for distributed access, streaming media, writing log files, and providing data storage for backup, disaster recovery, archiving, and analysis by on-premises or Azure-hosted services.” — Microsoft Learn.
Prerequisites
- Azure Subscription (Limited Free Credit)
- Azure Storage Account
- IDE (Visual Studio 2022 Community)
- Basic .NET Development knowledge
Creating the Storage Account and Containers
An Azure Subscription is required for this walk through, you can create a free subscription here.
Login to your Azure portal and create a new Storage Account.
I will not go through the details of creating a storage account and the configuration options that are offered by Microsoft. I will select the cheapest default values available for the sake of this post.
You can read the documentation for more information about configuration and pricing on Microsoft Learn
From the settings, click on “Containers” and create a container called “publicfiles”. I will use this container to store all files that will be made public. A use case for this might be a web app that delivers content to users.
Upload some files into the “publicfiles” container.
In order to connect to this storage account, a connection string is needed. Copy the connection string from the access keys tab
Create a .Net Web App
Open visual studio or you favorite code editor and create a new ASP.NET Core Web Application Project.
If you are new to Blazor, Check out these blog posts.
From .NET to the Web: Building a Blazor Front-End Part 2 — Full CRUD Application
Blazor Server App Authentication — Simplifying Authentication in .NET Core API with JWT — Part II
Next, open NuGet Package Manager and search for Azure.Storage.Blobs
Open the appsettings.json file and add the storage connection string.
"ConnectionStrings": { "StorageAccount": "[Paste your connection string here]" }
Create a new folder called “Services” and add a class called StorageService. This class with have all the logic for connecting to Azure Storage, and reading / writing blobs.
This class has two methods
- GetAccountName: returns the name of the storage account.
- GetBlobsInContainer: takes the container name as an argument and returns the list of blob names.
This class will be modified later to return more information about the blobs.
public class StorageService { private readonly IConfiguration _configuration; private readonly BlobServiceClient _blobServiceClient; public StorageService(IConfiguration configuration) { _configuration = configuration; _blobServiceClient = new BlobServiceClient(_configuration.GetConnectionString("StorageAccount")); } public string GetAccountName() { return _blobServiceClient.AccountName; } internal List<string> GetBlobsInContainerAsync(string containerName) { var containerClient = _blobServiceClient.GetBlobContainerClient(containerName); var blobs = containerClient.GetBlobs(); List<string> result = []; foreach(var blob in blobs) { result.Add(blob.Name); } return result; } }
The next step is to display the storage account name and the names of the blobs on the page. To do so, create an instance of the Storage Service class and call its methods.
public class IndexModel : PageModel { private readonly StorageService _storageService; public string? AccountName { get; private set; } public List<string>? Blobs{ get; private set; } public IndexModel(StorageService storageService) { _storageService = storageService; } public void OnGet() { GetAccountName(); GetBlobs(); } private void GetBlobs() { Blobs = _storageService.GetBlobsInContainer("publicfiles"); } public void GetAccountName() { AccountName = _storageService.GetAccountName(); } }
<div class="text-center"> <h1 class="display-4">Welcome</h1> <div> <p><h3>@Model.AccountName</h3></p> <p> <div> <table class="table"> <tbody> @foreach (var blob in Model.Blobs) { <tr> <td>@blob</td> </tr> } </tbody> </table> </div> </p> </div> </div>
Warning: The page will not look pretty, but it works 😃
Adding more details from the blobs
Create a folder called “Model” and add a record called AzureBlobItem. This record will contain the blob item properties that will be fetched from the storage account.
public record AzureBlobItem { public string? Name { get; set; } public string? Href { get; set; } }
Update the GetBlobsInContainer method in the StorageService class to fetch the required blob details.
internal List<AzureBlobItem> GetBlobsInContainer(string containerName) { var containerClient = _blobServiceClient.GetBlobContainerClient(containerName); var blobs = containerClient.GetBlobs(); List<AzureBlobItem> result = []; foreach(var blob in blobs) { var blobClient = containerClient.GetBlobClient(blob.Name); result.Add(new AzureBlobItem() { Name = blobClient.Name, Href = blobClient.Uri.ToString() }); } return result; }
Warning: It’s not going to be pretty, but again, it works! 😃
SeeSharp
There you have it, I just created a simple web application that fetches blobs from Azure Blob Storage. In Part 2, I will add “Upload” functionality. Stay Tuned.
You can check the code under this GitHub Repository
This blog is part of Microsoft Azure Week! Find more similar blogs on our Microsoft Azure Landing page here.
About the author:
Senior software engineer with over 12 years of experience in Microsoft Technologies and a successful track record as a software development manager. I have led development teams on more than 15 product launches, utilizing my project management expertise and technical skills to achieve results and meet team objectives.
Reference:
Yazbek, H. (2024) Reading/Writing Blobs from/Into Azure Blob Storage Using .Net Core – Part 1. Available at: Reading/Writing Blobs from/Into Azure Blob Storage Using .Net Core – Part 1 | LinkedIn [Accessed on 24/06/2024]