Upload Data to Azure Blob Storage

This article explains how to upload data to Azure Blob Storage using various methods, including the Azure portal, Azure CLI, Azure PowerShell, and client libraries.

Prerequisites
  • An Azure subscription.
  • A storage account.
  • A container within your storage account.

1. Using the Azure Portal

The Azure portal provides a user-friendly interface for uploading blobs.

  1. Navigate to your storage account in the Azure portal.
  2. Select the container you want to upload data to.
  3. Click the Upload button.
  4. Choose the files or folder you wish to upload.
  5. Configure any additional settings (e.g., access tier, block size) and click Upload.

2. Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources.

To upload a single blob:

az storage blob upload \
    --account-name  \
    --container-name  \
    --name  \
    --file  \
    --auth-mode login
    

To upload a folder (recursive):

az storage blob upload-batch \
    --account-name  \
    --destination  \
    --source  \
    --auth-mode login
    

Replace the placeholders with your actual storage account name, container name, blob name, and local file/folder path.

3. Using Azure PowerShell

Azure PowerShell provides cmdlets for scripting Azure management tasks.

To upload a single blob:

Set-AzStorageBlobContent `
    -Container  `
    -File  `
    -Blob  `
    -Context (New-AzStorageContext -StorageAccountName "" -StorageAccountKey "")
    

To upload a folder (recursive):

Get-ChildItem  -Recurse | ForEach-Object {
        if (!$_.PSIsContainer) {
            $blobName = $_.FullName.Substring($folder.FullName.Length).Trim('\')
            Set-AzStorageBlobContent `
                -Container  `
                -File $_.FullName `
                -Blob $blobName `
                -Context (New-AzStorageContext -StorageAccountName "" -StorageAccountKey "")
        }
    }
    

Note: It is recommended to use managed identities or Azure AD authentication where possible instead of storage account keys.

4. Using Client Libraries

Azure provides SDKs for various programming languages to interact with Blob Storage.

Here's a C# example for uploading a block blob:

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

// Replace with your actual connection string
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string containerName = "my-container";
string blobName = "my-blob.txt";
string filePath = "path/to/your/local/file.txt";

// Create a BlobServiceClient object
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

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

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

using (FileStream uploadFileStream = File.OpenRead(filePath))
{
    // Upload the file
    var result = await blobClient.UploadAsync(uploadFileStream, true);
    Console.WriteLine($"Upload successful: {result.GetRawResponse().Status}");
}

Similar examples are available for Python, Java, Node.js, and Go in the official Azure SDK documentation.

Tip: For large files, consider using block blobs and uploading them in parallel to improve performance.

Choosing the Right Method