Introduction

Azure Blob Storage is a scalable cloud storage solution provided by Microsoft Azure that is widely used for storing large amounts of unstructured data such as images, videos, documents, and backups. In modern .NET applications, Azure Blob Storage is commonly used to handle file uploads and downloads efficiently and securely.

This article explains how to use Azure Blob Storage in .NET to upload and download files with step-by-step examples, best practices, and real-world use cases.

What is Azure Blob Storage?

Azure Blob Storage is a service designed for storing binary large objects (blobs). It allows applications to store and retrieve files from the cloud using REST APIs or SDKs.

Key Features

Prerequisites

Before starting, make sure you have:

Step 1: Install Azure Blob Storage NuGet Package

Install the required package using the following command:

dotnet add package Azure.Storage.Blobs

Step 2: Configure Connection String

Get the connection string from your Azure Storage Account and store it securely (e.g., appsettings.json).

{
  "AzureStorage": {
    "ConnectionString": "your_connection_string_here",
    "ContainerName": "mycontainer"
  }
}

Step 3: Upload File to Azure Blob Storage

Example Code

using Azure.Storage.Blobs;

public class BlobService
{
    private readonly string _connectionString = "your_connection_string";
    private readonly string _containerName = "mycontainer";

    public async Task UploadFileAsync(string filePath)
    {
        var blobServiceClient = new BlobServiceClient(_connectionString);
        var containerClient = blobServiceClient.GetBlobContainerClient(_containerName);
        var blobClient = containerClient.GetBlobClient(Path.GetFileName(filePath));

        using (FileStream uploadFileStream = File.OpenRead(filePath))
        {
            await blobClient.UploadAsync(uploadFileStream, true);
        }
    }
}

This code uploads a file from the local system to Azure Blob Storage.

Step 4: Download File from Azure Blob Storage

Example Code

public async Task DownloadFileAsync(string blobName, string downloadPath)
{
    var blobServiceClient = new BlobServiceClient(_connectionString);
    var containerClient = blobServiceClient.GetBlobContainerClient(_containerName);
    var blobClient = containerClient.GetBlobClient(blobName);

    await blobClient.DownloadToAsync(downloadPath);
}

This method downloads a file from Azure Blob Storage to a local path.

Step 5: Create Container (Optional)

public async Task CreateContainerAsync()
{
    var blobServiceClient = new BlobServiceClient(_connectionString);
    var containerClient = blobServiceClient.GetBlobContainerClient(_containerName);

    await containerClient.CreateIfNotExistsAsync();
}

Real-World Use Case

In a web application, users can upload profile images or documents. Instead of storing files on the server, the application uploads them to Azure Blob Storage and stores only the file URL in the database. This improves scalability, security, and performance.

Best Practices

Common Errors and Fixes

Summary

Azure Blob Storage provides a reliable and scalable way to store and manage files in .NET applications. By using the Azure.Storage.Blobs SDK, developers can easily upload and download files with minimal code while ensuring security and performance. Integrating Azure Blob Storage into your application helps build cloud-ready systems that can handle large volumes of data efficiently.