Upload and Retrieve Blobs in Azure Storage

This document guides you through the process of uploading and retrieving blobs (unstructured data) using Azure Storage. Azure Blob Storage is a service that stores unstructured data like text or binary data. Any amount of data can be stored and accessed from anywhere by using HTTP or HTTPS.

1. Prerequisites

Before you begin, ensure you have the following:

2. Uploading Blobs

You can upload blobs to Azure Blob Storage using various methods, including the Azure portal, Azure CLI, or Azure SDKs.

2.1. Using Azure CLI

The Azure CLI provides a simple way to manage your Azure resources. To upload a blob, use the az storage blob upload command.

Example: Upload a file to a container
az storage blob upload --account-name mystorageaccount --container-name mycontainer --name myblob.txt --file mylocalfile.txt --auth-mode login

Replace mystorageaccount, mycontainer, myblob.txt, and mylocalfile.txt with your specific values.

Note: The --auth-mode login parameter uses your Azure AD credentials for authentication. You can also use a connection string or shared access signature (SAS) token.

2.2. Using Azure SDK (Python Example)

Here's a Python example demonstrating how to upload a blob using the Azure Blob Storage SDK:

Python Code: Upload Blob

from azure.storage.blob import BlobServiceClient

# Replace with your storage account connection string
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
# Replace with your container name
container_name = "mycontainer"
# Replace with the local file path
local_file_path = "mylocalfile.txt"
# Replace with the desired blob name in Azure
blob_name = "myblob_sdk.txt"

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)

    print(f"Uploading {local_file_path} to {blob_name}...")
    with open(local_file_path, "rb") as data:
        blob_client.upload_blob(data)
    print("Upload successful!")

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

Make sure you have installed the Azure Blob Storage SDK for Python:

pip install azure-storage-blob

3. Retrieving Blobs

Once blobs are uploaded, you can retrieve them in a similar fashion.

3.1. Using Azure CLI

Use the az storage blob download command to download a blob.

Example: Download a blob
az storage blob download --account-name mystorageaccount --container-name mycontainer --name myblob.txt --file downloaded_blob.txt --auth-mode login

This command downloads myblob.txt from the specified container into a local file named downloaded_blob.txt.

3.2. Using Azure SDK (Python Example)

Here's how to download a blob using the Azure Blob Storage SDK for Python:

Python Code: Download Blob

from azure.storage.blob import BlobServiceClient

# Replace with your storage account connection string
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
# Replace with your container name
container_name = "mycontainer"
# Replace with the blob name in Azure
blob_name = "myblob_sdk.txt"
# Replace with the desired local file path to save the blob
local_file_path = "downloaded_sdk_blob.txt"

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)

    print(f"Downloading {blob_name} to {local_file_path}...")
    with open(local_file_path, "wb") as download_file:
        download_file.write(blob_client.download_blob().readall())
    print("Download successful!")

except Exception as ex:
    print('Exception:')
    print(ex)
            
Tip: For large blobs, consider using the download_blob(max_concurrency=...) with appropriate concurrency settings to improve download performance.

4. Security Considerations

When handling blob uploads and downloads, always consider security:

Next Steps

Explore other Azure Blob Storage features such as managing access tiers, using access policies, and implementing soft delete for data protection.

Azure Blob Storage Architecture Diagram
Conceptual diagram of Azure Blob Storage interaction.