Uploading Blobs to Azure Storage

This article details how to upload a blob to Azure Blob Storage using various methods.

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. This guide will walk you through the common scenarios for uploading blobs using the Azure SDKs and Azure CLI.

Prerequisites

Before you begin, ensure you have the following:

Methods for Uploading Blobs

Using Azure SDKs (Python Example)

The Azure SDKs provide a robust way to interact with Azure services. Here's an example using the Python SDK to upload a block blob.

Note: Replace <YOUR_STORAGE_ACCOUNT_NAME>, <YOUR_STORAGE_ACCOUNT_KEY>, <YOUR_CONTAINER_NAME>, and local_file_path.txt with your actual values.

from azure.storage.blob import BlobServiceClient, ContentSettings

# Replace with your actual storage account name and key
connection_string = f"DefaultEndpointsProtocol=https;AccountName=;AccountKey=;EndpointSuffix=core.windows.net"
container_name = ""
local_file_name = "local_file_path.txt"
blob_name = "my-uploaded-blob.txt"

try:
    # Create a BlobServiceClient object
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)

    # Get a client to interact with a specific container
    container_client = blob_service_client.get_container_client(container_name)

    # Create a local file for demonstration
    with open(local_file_name, "w") as file:
        file.write("This is the content of the blob.")

    # Upload the blob
    with open(local_file_name, "rb") as data:
        blob_client = container_client.upload_blob(name=blob_name, data=data, overwrite=True)
        print(f"Blob '{blob_name}' uploaded successfully.")
        print(f"Blob URL: {blob_client.url}")

except Exception as ex:
    print('Exception:')
    print(ex)

# Optional: Clean up the local file
import os
if os.path.exists(local_file_name):
    os.remove(local_file_name)
    print(f"Local file '{local_file_name}' removed.")

Other SDKs

Similar examples exist for other languages like .NET, Java, Node.js, and JavaScript. Please refer to the official Azure SDK documentation for specific language implementations.

Using Azure CLI

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

First, log in to your Azure account:

az login

Then, use the az storage blob upload command:

az storage blob upload \
    --account-name  \
    --account-key  \
    --container-name  \
    --name my-uploaded-blob-cli.txt \
    --file ./local_file_path.txt \
    --overwrite

If you have configured a default context or are using managed identity/SAS tokens, you might not need to provide --account-key.

You can also upload to a specific directory (virtual folder) within the container:

az storage blob upload \
    --account-name  \
    --container-name  \
    --name my-folder/my-uploaded-blob-cli.txt \
    --file ./local_file_path.txt \
    --overwrite

Blob Types and Upload Methods

Azure Blob Storage supports three types of blobs:

The upload methods discussed primarily apply to block blobs. For append blobs, you would use specific append operations from the SDKs.

Best Practices

For more advanced scenarios like uploading large files in chunks (parallel upload), managing snapshots, or using SAS tokens, please refer to the conceptual documentation on uploading blobs.