Azure Blob Storage in C#

Introduction

Azure Blob Storage is a scalable cloud storage service that allows developers to store and manage vast amounts of unstructured data. It's an essential component for various applications that deal with images, videos, documents, backups, and more. With its reliability, durability, and accessibility, Azure Blob Storage is a go-to choice for cloud-based storage solutions.

Overview of Azure Blob Storage

Azure Blob Storage organizes data into containers. Each container can hold an unlimited number of blobs, which are the actual files. Blobs can be of three types: Block blobs, Append blobs, and Page blobs. Block blobs are suitable for general-purpose storage of files, while Append blobs are ideal for append-only scenarios like logging. Page blobs are optimized for random read/write operations and are often used to store virtual hard drive (VHD) files.

Working with Azure Blob Storage in C#

To utilize Azure Blob Storage in C#, you'll need the Azure Storage SDK. If you're using Visual Studio, you can install the required package via the NuGet Package Manager. Here's a step-by-step guide demonstrating how to perform basic operations:

1. Setting Up Azure Storage Connection

First, ensure you have an Azure subscription and have created a Storage Account in the Azure portal. Once you have the account details, install the Azure.Storage.Blobs package.

using Azure.Storage.Blobs;

public class AzureBlobStorageService
{
    private string connectionString = "YourStorageAccountConnectionString";
    private BlobServiceClient blobServiceClient;

    public AzureBlobStorageService()
    {
        blobServiceClient = new BlobServiceClient(connectionString);
    }
}

Replace "YourStorageAccountConnectionString" with the connection string for your Azure Storage Account.

2. Creating a Blob Container

Now, let's create a container within the storage account.

public async Task CreateContainerAsync(string containerName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    await containerClient.CreateIfNotExistsAsync();
}

3. Uploading a Blob

Here's an example of how you can upload a file (blob) to a container.

public async Task UploadBlobAsync(string containerName, string blobName, string filePath)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

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

4. Downloading a Blob

To retrieve a blob from the container, use the following method.

public async Task DownloadBlobAsync(string containerName, string blobName, string downloadPath)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    BlobDownloadInfo blobDownloadInfo = await blobClient.DownloadAsync();

    using FileStream fs = File.OpenWrite(downloadPath);
    await blobDownloadInfo.Content.CopyToAsync(fs);
}

5. Listing Blobs in a Container

To list all the blobs within a container, you can use the following method.

public async Task<List<string>> ListBlobsAsync(string containerName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    var blobs = new List<string>();

    await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
    {
        blobs.Add(blobItem.Name);
    }

    return blobs;
}

This snippet retrieves the names of all blobs within the specified container and returns a list of strings containing the blob names.

6. Deleting a Blob

To delete a specific blob from a container.

public async Task DeleteBlobAsync(string containerName, string blobName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    await blobClient.DeleteIfExistsAsync();
}

This method deletes the specified blob from the container.

7. Deleting a Container

To remove an entire container from the storage account.

public async Task DeleteContainerAsync(string containerName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

    await containerClient.DeleteIfExistsAsync();
}

8. Accessing Blob Metadata

Fetching metadata associated with a specific blob.

public async Task<IDictionary<string, string>> GetBlobMetadataAsync(string containerName, string blobName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    BlobProperties properties = await blobClient.GetPropertiesAsync();
    return properties.Metadata;
}

This snippet fetches the metadata associated with the specified blob in the container.

Conclusion

Azure Blob Storage provides a reliable and scalable solution for storing various types of unstructured data in the cloud. Using C# with the Azure.Storage.Blobs package, developers can easily perform fundamental operations such as creating containers, uploading, and downloading blobs. This enables the seamless integration of cloud-based storage capabilities within applications. By following the provided code snippets, developers can harness the power of Azure Blob Storage in their C# applications.


Similar Articles