Upload and Retrieve Blobs with Azure Storage

Overview

Azure Blob Storage is a service that stores unstructured data such as text or binary data. Blobs can be any type of text or binary data, such as images, documents, or streaming media. A single Azure Storage account can store hundreds of terabytes of data.

This document guides you through the process of uploading files (blobs) to Azure Blob Storage and then retrieving them. We'll cover common scenarios and provide code examples using the Azure SDK for .NET and Azure CLI.

Prerequisites

1. Uploading Blobs

You can upload blobs to Azure Blob Storage using various methods. Here are two common ones:

1.1 Using Azure CLI

The Azure CLI provides a simple way to upload files. First, ensure you are logged into your Azure account:

az login

Then, use the az storage blob upload command:

az storage blob upload \
    --account-name  \
    --account-key  \
    --container-name  \
    --name  \
    --file 

Replace placeholders like <YOUR_STORAGE_ACCOUNT_NAME>, <YOUR_STORAGE_ACCOUNT_KEY>, <YOUR_CONTAINER_NAME>, <DESTINATION_BLOB_NAME>, and <LOCAL_FILE_PATH> with your actual values.

You can find your storage account name and key in the Azure portal under your storage account's "Access keys" section.

1.2 Using Azure SDK for .NET

This example demonstrates uploading a file using the Azure Blob Storage SDK for .NET. Make sure you have the Azure.Storage.Blobs NuGet package installed.

using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

public class BlobUploader
{
    public static async Task UploadFileAsync(string connectionString, string containerName, string blobName, string filePath)
    {
        // Create a BlobServiceClient object using the connection string
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

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

        // Get a reference to the blob
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        // Open the file to be uploaded
        using (FileStream uploadFileStream = File.OpenRead(filePath))
        {
            // Upload the file
            await blobClient.UploadAsync(uploadFileStream, true); // Overwrite if it already exists
            Console.WriteLine($"Successfully uploaded {blobName} to container {containerName}.");
        }
    }

    // Example usage:
    // public static async Task Main(string[] args)
    // {
    //     string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    //     string containerName = "mycontainer";
    //     string blobName = "myblob.txt";
    //     string filePath = "/path/to/your/local/file.txt";
    //     await UploadFileAsync(connectionString, containerName, blobName, filePath);
    // }
}

It's best practice to store your connection string securely, for example, using Azure Key Vault or application settings.

2. Retrieving Blobs

Once your blobs are stored, you can retrieve them. Here are methods for downloading:

2.1 Using Azure CLI

Use the az storage blob download command:

az storage blob download \
    --account-name  \
    --account-key  \
    --container-name  \
    --name  \
    --file 

Replace placeholders with your specific details. <BLOB_TO_DOWNLOAD_NAME> is the name of the blob in Azure Storage, and <LOCAL_DESTINATION_PATH> is where you want to save it locally.

2.2 Using Azure SDK for .NET

Download a blob using the Azure Blob Storage SDK for .NET:

using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

public class BlobDownloader
{
    public static async Task DownloadFileAsync(string connectionString, string containerName, string blobName, string filePath)
    {
        // Create a BlobServiceClient object using the connection string
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

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

        // Get a reference to the blob
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        // Download the blob to a local file
        using (FileStream downloadFileStream = File.Create(filePath))
        {
            await blobClient.DownloadToAsync(downloadFileStream);
            Console.WriteLine($"Successfully downloaded {blobName} to {filePath}.");
        }
    }

    // Example usage:
    // public static async Task Main(string[] args)
    // {
    //     string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    //     string containerName = "mycontainer";
    //     string blobName = "myblob.txt";
    //     string filePath = "/path/to/save/downloaded/file.txt";
    //     await DownloadFileAsync(connectionString, containerName, blobName, filePath);
    // }
}

3. Managing Containers

Before uploading or downloading, you typically need a container. Containers are logical groupings for your blobs.

3.1 Creating a Container using Azure CLI

az storage container create \
    --account-name  \
    --account-key  \
    --name 

3.2 Creating a Container using Azure SDK for .NET

using Azure.Storage.Blobs;
using System;
using System.Threading.Tasks;

public class ContainerManager
{
    public static async Task CreateContainerAsync(string connectionString, string containerName)
    {
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        await blobServiceClient.CreateBlobContainerAsync(containerName);
        Console.WriteLine($"Container '{containerName}' created successfully.");
    }

    // Example usage:
    // public static async Task Main(string[] args)
    // {
    //     string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    //     await CreateContainerAsync(connectionString, "new-documents");
    // }
}

4. Security Considerations

Securing your Azure Storage is crucial. Consider the following:

Never embed sensitive keys directly in client-side code (like JavaScript running in a browser).

5. Further Reading

Try it Out (Simulated Upload)

This section is a demonstration. Actual uploads require a real Azure Storage setup.