Upload and Download Blobs with Azure Storage

This document guides you through the process of uploading and downloading blobs using Azure Blob Storage. Azure Blob Storage is a cloud object storage solution that stores unstructured data like text or binary data. Blobs can be accessed from anywhere in the world via HTTP or HTTPS.

Prerequisites

Create a Blob Container

Before you can upload blobs, you need a container within your storage account. A container is a logical grouping of blobs.

Using Azure CLI

az storage container create --name mycontainer --account-name mystorageaccount --auth-mode login

Replace mycontainer with your desired container name and mystorageaccount with your storage account name.

Using Azure Portal

Navigate to your storage account in the Azure portal, select "Containers" under "Data storage", and click "+ Container".

Upload a Blob

You can upload various types of data as blobs, including files, images, and videos. The most common type is a block blob, which is ideal for storing large amounts of data.

Using Azure CLI

az storage blob upload --account-name mystorageaccount --container-name mycontainer --name myblob.txt --file /path/to/local/myblob.txt --auth-mode login

This command uploads the local file /path/to/local/myblob.txt to a blob named myblob.txt in the mycontainer container of your storage account.

Using SDKs (Python Example)

from azure.storage.blob import BlobServiceClient

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)

container_client = blob_service_client.get_container_client("mycontainer")

local_file_name = "myblob.txt"
source_file_path = "./local_files/" + local_file_name
blob_name = "my_uploaded_blob.txt"

with open(source_file_path, "rb") as data:
    container_client.upload_blob(name=blob_name, data=data)

print(f"Uploaded {local_file_name} to blob {blob_name}")

Remember to replace YOUR_AZURE_STORAGE_CONNECTION_STRING with your actual connection string, and adjust file paths and blob names as needed.

Download a Blob

Downloading blobs allows you to retrieve your data from Azure Storage.

Using Azure CLI

az storage blob download --account-name mystorageaccount --container-name mycontainer --name myblob.txt --file ./downloaded_files/myblob.txt --auth-mode login

This command downloads the blob named myblob.txt from mycontainer to the local path ./downloaded_files/myblob.txt.

Using SDKs (Python Example)

from azure.storage.blob import BlobServiceClient

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)

container_client = blob_service_client.get_container_client("mycontainer")

blob_name = "my_uploaded_blob.txt"
destination_file_path = "./downloaded_files/downloaded_blob.txt"

with open(destination_file_path, "wb") as download_file:
    download_file.write(container_client.download_blob(blob_name).readall())

print(f"Downloaded blob {blob_name} to {destination_file_path}")

Ensure you have the correct connection string, container name, blob name, and destination file path.

Next Steps

Explore more advanced blob operations:

Note: For production scenarios, consider using shared access signatures (SAS) or Azure Active Directory authentication for secure access to your blobs instead of directly using connection strings.