A comprehensive guide to uploading, downloading, and managing files using Azure Blob Storage.
Azure Blob Storage is Microsoft's object storage solution for the cloud. It is optimized for storing massive amounts of unstructured data, such as text or binary data. Unstructured data is data that doesn't adhere to a particular data model or definition, such as images, videos, audio files, or any other type of media file, documents, or backup data.
This document will guide you through the essential operations for working with files (blobs) in Azure Blob Storage, covering common use cases and providing code examples for popular programming languages and tools.
Before you can work with files, you need to connect to your Azure Storage account. This typically involves using your storage account name and one of its access keys, or a Shared Access Signature (SAS) token.
Note: For production environments, consider using Azure Active Directory (Azure AD) for authentication and authorization for enhanced security.
The Azure portal provides a user-friendly interface to upload, download, and manage blobs directly. Navigate to your storage account, select 'Containers', choose a container, and then click 'Upload' or 'Download' as needed.
The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources. You can use it for various blob operations.
# Login to your Azure account
az login
# Set your subscription (if you have multiple)
az account set --subscription ""
# List storage accounts
az storage account list
# Create a container (if it doesn't exist)
az storage container create --name mycontainer --account-name mystorageaccount --public-access off
Uploading files, often referred to as uploading blobs, is a fundamental operation. Azure Blob Storage supports uploading various types of files, including block blobs, page blobs, and append blobs. For most file storage scenarios, block blobs are the most common choice.
# Upload a single file
az storage blob upload --account-name mystorageaccount --container-name mycontainer --file "local/path/to/your/file.txt" --name "destination/blob/name.txt"
# Upload multiple files from a directory
az storage blob upload-batch --account-name mystorageaccount --destination mycontainer --source "local/directory/" --pattern "*.txt"
from azure.storage.blob import BlobServiceClient
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client("mycontainer")
# Upload a file
blob_name = "destination/blob/name.txt"
local_file_path = "local/path/to/your/file.txt"
with open(local_file_path, "rb") as data:
container_client.upload_blob(name=blob_name, data=data)
print(f"Uploaded {blob_name} to container.")
YOUR_AZURE_STORAGE_CONNECTION_STRING with your actual connection string found in your storage account's access keys in the Azure portal.
Downloading files from Azure Blob Storage is as straightforward as uploading. You can download a single blob to a local file or stream its content.
# Download a single blob
az storage blob download --account-name mystorageaccount --container-name mycontainer --name "destination/blob/name.txt" --file "local/path/to/save/file.txt"
# Download all blobs from a container
az storage blob download-batch --account-name mystorageaccount --destination "local/directory/" --container-name mycontainer
from azure.storage.blob import BlobServiceClient
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client("mycontainer")
blob_name = "destination/blob/name.txt"
local_file_path = "local/path/to/save/downloaded_file.txt"
# Download blob content
blob_client = container_client.get_blob_client(blob_name)
with open(local_file_path, "wb") as download_file:
download_file.write(blob_client.download_blob().readall())
print(f"Downloaded {blob_name} to {local_file_path}.")
Beyond uploading and downloading, you can perform various management operations on your blobs.
View the contents of a container.
az storage blob list --account-name mystorageaccount --container-name mycontainer
# Using Azure SDK for Python
for blob_item in container_client.list_blobs():
print(f"Blob name: {blob_item.name}")
Remove blobs from a container.
az storage blob delete --account-name mystorageaccount --container-name mycontainer --name "blob/to/delete.txt"
# Using Azure SDK for Python
container_client.delete_blob("blob/to/delete.txt")
print("Blob deleted successfully.")
Copy blobs within the same storage account or between different storage accounts.
# Copy within the same account
az storage blob copy --account-name mystorageaccount --source-container mycontainer --source-blob "source/blob.txt" --destination-container mycontainer --destination-blob "copied/blob.txt"
Customize blob properties like content type or add custom metadata.
# Using Azure SDK for Python
blob_client = container_client.get_blob_client("myblob.txt")
blob_client.set_blob_properties(content_settings={'content_type': 'text/plain'})
blob_client.set_blob_metadata({'custom-key': 'custom-value'})
print("Blob properties and metadata updated.")
az storage blob upload
az storage blob download
BlobClient.upload_blob()
BlobClient.download_blob()