Efficiently Downloading Files from Azure Blob Storage in C#

Efficiently Downloading Files from Azure Blob Storage in C#

Managing files in the cloud has become an essential part of modern applications. Azure Blob Storage provides a scalable solution for storing and retrieving large amounts of unstructured data. In this post, we’ll explore how to efficiently download files from Azure Blob Storage using C#. We’ll walk through a sample method that demonstrates how to accomplish this task.

Understanding the Code

Below is a C# method designed to download a file from Azure Blob Storage. This method leverages the Azure SDK to interact with blob storage, retrieve the desired file, and return it as a stream.

public async Task<Stream> DownloadFile(string folderPath, string fileName)
{
    // Retrieve the Azure Blob Storage URL and container name from configuration
    string blobUrl = _config[Constants.BlobUrl];
    string containerName = _config[Constants.BlobcontainerName];

    // Create a BlobServiceClient using the Blob Storage URL and default Azure credentials
    var blobServiceClient = new BlobServiceClient(new Uri(blobUrl), new DefaultAzureCredential());

    // Get a BlobContainerClient to interact with the specified container
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

    // Find the blob that matches the provided folder path and file name
    BlobItem? blobData = containerClient.GetBlobs(prefix: folderPath)
                                        .Where(x => x.Name.EndsWith(fileName))
                                        .FirstOrDefault();

    // If the blob is found
    if (blobData != null)
    {
        // Get a BlobClient to interact with the specific blob
        var blobClient = containerClient.GetBlobClient(blobData.Name);
        
        if (blobClient != null)
        {
            // Download the blob's content asynchronously
            var downloadContent = await blobClient.DownloadAsync();
            // Return the content as a Stream
            return downloadContent.Value.Content;
        }
    }

    // Return null if the blob was not found
    return null;
}

Key Components of the Method

  1. Configuration Retrieval: The method starts by retrieving the Blob Storage URL and container name from the application’s configuration settings.
  2. BlobServiceClient Initialization: A BlobServiceClient is created using the storage URL and Azure credentials. This client is used to interact with the Blob Storage service.
  3. BlobContainerClient: This client is used to interact with a specific container within the Blob Storage account. It allows us to perform operations such as listing blobs and getting blob clients.
  4. Finding the Blob: The method searches for a blob within the specified folder path that matches the provided file name. This is done using the GetBlobs method with a prefix filter.
  5. Downloading the Blob: If the blob is found, a BlobClient is created to access it. The blob’s content is then downloaded asynchronously and returned as a Stream.
  6. Handling Non-existent Blobs: If the blob is not found, the method returns null, indicating that no file was retrieved.

Conclusion

This method provides a straightforward approach to downloading files from Azure Blob Storage using the Azure SDK for .NET. It efficiently handles the retrieval of file content, including the use of asynchronous operations to avoid blocking calls. By leveraging Azure’s robust API and SDK, developers can build scalable and efficient applications that interact with cloud storage seamlessly.