Implementing Azure Blob Storage — API application

DotNet Full Stack Dev
4 min readMay 1, 2024

--

Azure Blob Storage is a cloud-based object storage service provided by Microsoft Azure. It allows you to store and manage large amounts of unstructured data, such as text or binary data, in the cloud. Blobs are optimized for storing and accessing large amounts of data, making them ideal for scenarios such as serving images or videos to web applications, storing backups, and archiving data.

Embark on a journey of continuous learning and exploration with DotNet-FullStack-Dev. Uncover more by visiting our https://dotnet-fullstack-dev.blogspot.com reach out for further information.

Components of Azure Blob Storage

  1. Storage Account: A storage account is a globally unique namespace in Azure that provides a way to access and manage your Azure Storage services. It serves as a container for your blobs, queues, tables, and files.
  2. Blob Container: Blob containers are logical containers within a storage account that organize and store blobs. You can create multiple containers within a storage account to partition your data and manage access control and lifecycle policies for the blobs they contain.
  3. Blob: A blob is a binary large object that represents the data stored in Azure Blob Storage. Blobs can be of different types:
  • Block Blobs: Optimized for streaming and storing large amounts of text or binary data, such as documents or images.
  • Append Blobs: Designed for append-only scenarios, such as logging or data analytics, where data is frequently appended to the blob.
  • Page Blobs: Used for random read/write operations and are commonly used for storing virtual hard disk (VHD) files for Azure Virtual Machines.
  • Blob Service: The Blob Service is the primary endpoint for accessing and managing blobs within a storage account. It provides operations for creating, listing, reading, writing, and deleting blobs and containers.

Key Operations in Azure Blob Storage

  1. Upload Blob: Upload a blob to Azure Blob Storage by providing the blob’s content and metadata to the Blob Service. This operation stores the blob in the specified container.
  2. Download Blob: Retrieve the contents of a blob from Azure Blob Storage by specifying the blob’s unique identifier (URI). This operation retrieves the blob’s data and metadata.
  3. Delete Blob: Remove a blob from Azure Blob Storage by specifying its unique identifier. This operation permanently deletes the blob from the storage account.
  4. Access Control: Azure Blob Storage provides mechanisms for controlling access to blobs and containers, including Shared Access Signatures (SAS) and access policies. These mechanisms allow you to grant limited access to specific blobs or containers to users or applications.

Step 1: Set Up Azure Storage Account

  1. Create a Storage Account: This is the first step in using Azure Blob Storage. Navigate to the Azure portal and create a new Storage Account. This account serves as a unique namespace in Azure for your data.
  2. Create a Blob Container: Within your Storage Account, you need to create one or more Blob Containers. These containers organize your blobs and provide a way to manage access control and lifecycle policies for the blobs they contain.

Step 2: Install Azure.Storage.Blobs Package

The Azure.Storage.Blobs package is a .NET library provided by Microsoft for interacting with Azure Blob Storage. You can install it via NuGet Package Manager or .NET CLI. This package provides classes and methods for working with blobs, containers, and other aspects of Azure Blob Storage.

dotnet add package Azure.Storage.Blobs

Step 3: Initialize BlobServiceClient

The BlobServiceClient is the primary client object for interacting with Azure Blob Storage. It represents the endpoint for your storage account and provides methods for creating, listing, and deleting containers, as well as other administrative tasks.

using Azure.Storage.Blobs;

BlobServiceClient blobServiceClient = new BlobServiceClient("your_connection_string");

Step 4: Upload a Blob

To upload a blob to Azure Blob Storage, you first need to get a reference to the Blob Container where you want to store the blob. Then, you can create a BlobClient object representing the blob within that container and use its UploadAsync method to upload the blob's content.

BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("your_container_name");
BlobClient blobClient = containerClient.GetBlobClient("your_blob_name");

using (var fileStream = File.OpenRead("path_to_your_file"))
{
await blobClient.UploadAsync(fileStream, true);
}

Step 5: Download a Blob

Downloading a blob from Azure Blob Storage involves retrieving the blob’s content from the storage service and saving it to a local file or memory stream. You can use the DownloadAsync method of the BlobClient to initiate the download operation and obtain a BlobDownloadInfo object containing the blob's content.

BlobDownloadInfo download = await blobClient.DownloadAsync();
using (var fileStream = File.OpenWrite("destination_path"))
{
await download.Content.CopyToAsync(fileStream);
}

Step 6: Delete a Blob

Deleting a blob from Azure Blob Storage is straightforward. You simply need to call the DeleteAsync method on the BlobClient representing the blob you want to delete. This permanently removes the blob from the storage account.

await blobClient.DeleteAsync();

Step 7: Access Control (Optional)

Azure Blob Storage provides various mechanisms for controlling access to your blobs and containers. You can generate Shared Access Signatures (SAS) to grant limited access to specific blobs or containers, or you can define access policies that specify permissions for different types of access (e.g., read, write, delete).

Conclusion

Integrating Azure Blob Storage into your .NET application enables you to leverage the scalability, durability, and accessibility of cloud storage for managing your application’s data. By following the steps outlined above and using the Azure.Storage.Blobs package, you can easily upload, download, and manage blobs in Azure Blob Storage from your .NET code.

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

No responses yet