Using Azure Blob Storage with Python
This document provides a comprehensive guide on how to interact with Azure Blob Storage using the Azure SDK for Python. You'll learn how to perform common operations such as creating containers, uploading, downloading, and deleting blobs, as well as managing access to your storage resources.
- An Azure subscription.
- An Azure Storage account.
- Python 3.7 or later installed.
- The Azure Blob Storage client library for Python installed:
pip install azure-storage-blob
1. Connecting to Blob Storage
To interact with Blob Storage, you'll need to create a BlobServiceClient object. You can authenticate using a connection string or a shared access signature (SAS) token. For development, a connection string is often the easiest method.
Using Connection String
Retrieve your storage account connection string from the Azure portal (under your storage account's "Access keys" or "Connection strings" section).
from azure.storage.blob import BlobServiceClient
connection_string = "YOUR_STORAGE_ACCOUNT_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
Using Azure AD Credentials (Recommended for Production)
For production scenarios, it's highly recommended to use Azure Active Directory (Azure AD) for authentication. This involves using a credential object like DefaultAzureCredential.
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
account_url = "https://your_account_name.blob.core.windows.net"
credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(account_url=account_url, credential=credential)
2. Working with Containers
Containers are logical groupings of blobs within your storage account.
Create a Container
container_client = blob_service_client.create_container("my-python-container")
print(f"Container 'my-python-container' created.")
Get a Container Client
If the container already exists, you can get a client to interact with it:
container_client = blob_service_client.get_container_client("my-python-container")
List Containers
print("Listing containers:")
for container in blob_service_client.list_containers():
print(f"- {container.name}")
3. Working with Blobs
Blobs are individual files stored in a container.
Upload a Blob
You can upload a local file to a container.
from azure.storage.blob import BlobClient
# Upload a local file
local_file_path = "path/to/your/local/file.txt"
blob_name = "my-uploaded-file.txt"
blob_client = blob_service_client.get_blob_client(container="my-python-container", blob=blob_name)
with open(local_file_path, "rb") as data:
blob_client.upload_blob(data)
print(f"Blob '{blob_name}' uploaded successfully.")
Download a Blob
download_file_path = "path/to/save/downloaded/file.txt"
with open(download_file_path, "wb") as download_file:
download_file.data = blob_client.download_blob()
download_file.write(download_file.data)
print(f"Blob '{blob_name}' downloaded to '{download_file_path}'.")
List Blobs in a Container
print(f"Listing blobs in container 'my-python-container':")
for blob in container_client.list_blobs():
print(f"- {blob.name}")
Delete a Blob
blob_client.delete_blob()
print(f"Blob '{blob_name}' deleted.")
Delete a Container
Note: Deleting a container also deletes all blobs within it.
blob_service_client.delete_container("my-python-container")
print("Container 'my-python-container' deleted.")
4. Advanced Operations
Uploading Larger Files (using `upload_blob` with `max_single_put_size`)
For very large files, the SDK can automatically handle chunking. You can also control the chunk size if needed.
# Example of uploading a large file (SDK handles chunking automatically)
large_file_path = "path/to/your/large_file.zip"
large_blob_name = "my-large-file.zip"
blob_client_large = blob_service_client.get_blob_client(container="my-python-container", blob=large_blob_name)
with open(large_file_path, "rb") as data:
blob_client_large.upload_blob(data, overwrite=True) # overwrite=True if the blob already exists
print(f"Large blob '{large_blob_name}' uploaded.")
Downloading a Range of a Blob
# Download a specific range of bytes from a blob
blob_client_range = blob_service_client.get_blob_client(container="my-python-container", blob="my-uploaded-file.txt")
offset = 10 # Start byte
length = 20 # Number of bytes to download
blob_data = blob_client_range.download_blob(offset=offset, length=length).readall()
print(f"Downloaded {len(blob_data)} bytes from blob.")
Conclusion
You've now seen the fundamental operations for working with Azure Blob Storage using Python. The Azure SDK for Python provides a rich set of features for managing your data, offering flexibility and robustness for your cloud storage needs.
Refer to the official Azure Blob Storage SDK for Python documentation for more detailed information and advanced use cases.