Upload blobs to Azure Blob Storage

This guide will walk you through the process of uploading blobs to Azure Blob Storage using various methods, including the Azure CLI, Azure SDKs, and AzCopy.

Prerequisites

Methods for Uploading Blobs

1. Using the Azure CLI

The Azure Command-Line Interface (CLI) provides a straightforward way to manage your Azure resources, including uploading blobs.

Upload a single blob

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

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

Upload multiple blobs recursively

To upload all files from a local directory to a container, use the --recursive flag.

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

Note: The --auth-mode login uses your Azure CLI login credentials. You can also use other authentication methods like connection strings or shared access signatures (SAS) tokens.

2. Using Azure SDKs

Azure provides SDKs for various programming languages to interact with Azure services. Here's a basic example using the Azure Blob Storage SDK for Python.

Python Example

# Install the SDK: pip install azure-storage-blob

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import os

connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
container_name = ""
local_file_path = ""
blob_name = os.path.basename(local_file_path)

try:
    # Create the BlobServiceClient object
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)

    # Get a client to interact with a specific blob
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

    # Upload the blob
    with open(local_file_path, "rb") as data:
        blob_client.upload_blob(data)

    print(f"Successfully uploaded {local_file_path} to {blob_name} in container {container_name}.")

except Exception as ex:
    print('Exception: {}'.format(ex))

Ensure you have set your connection string as an environment variable named AZURE_STORAGE_CONNECTION_STRING.

3. Using AzCopy

AzCopy is a command-line utility designed for high-performance copying of data to and from Azure Blob Storage and Azure Files.

Upload a file

azcopy copy "" "https://.blob.core.windows.net//"

You'll typically need a SAS token or an account key for authentication with AzCopy.

Upload a directory

azcopy copy "" "https://.blob.core.windows.net//" --recursive=true

Important: For AzCopy, replace <YOUR_SAS_TOKEN> with your actual Shared Access Signature token. You can generate a SAS token from the Azure portal.

Best Practices for Uploading

Get Started with Azure Blob Storage