Azure Storage SDK (Legacy)
This is the legacy Microsoft Azure Storage SDK for Python, currently at version 0.37.0. This package unified clients for Blobs, Queues, and Files. It is superseded by the modular, actively developed client libraries (`azure-storage-blob`, `azure-storage-queue`, `azure-storage-file-share`) which offer a more modern API, better async support, and integration with Azure Identity. This package is no longer actively maintained.
Warnings
- breaking The `azure-storage` package (v0.x) has been entirely superseded by the modular client libraries (`azure-storage-blob`, `azure-storage-queue`, `azure-storage-file-share`, all v12.x+). There is no direct upgrade path; code written for `azure-storage` will require a complete rewrite to use the new client libraries due to different class names, API signatures, and object models.
- deprecated The `azure-storage` package itself is deprecated and no longer receives active development or new features. Using it for new projects is strongly discouraged.
- gotcha Authentication methods and client initialization patterns are completely different between the legacy `azure-storage` (v0.x) and the modern modular SDKs (v12.x+). The modern SDKs leverage `azure-identity` for robust, token-based authentication (e.g., `DefaultAzureCredential`), alongside connection strings or SAS tokens.
- gotcha The Python Azure SDK underwent a complete redesign in its V2 efforts, resulting in entirely new client classes and method names. For instance, `BlockBlobService` from `azure-storage` is replaced by `BlobServiceClient`, `ContainerClient`, and `BlobClient` in `azure-storage-blob`.
Install
-
pip install azure-storage -
pip install azure-storage-blob azure-storage-queue azure-storage-file-share
Imports
- BlockBlobService
from azure.storage.blob.baseblobservice import BaseBlobService # Note: This is for the legacy azure-storage package (v0.x). # For new development, use BlobServiceClient from azure-storage-blob (v12.x+).
- BlobServiceClient
from azure.storage.blob import BlobServiceClient
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
# Recommended: Use the modern azure-storage-blob package
# Ensure AZURE_STORAGE_CONNECTION_STRING or AZURE_STORAGE_ACCOUNT_NAME is set
# Or configure Azure Identity credentials for DefaultAzureCredential
try:
# Option 1: Using connection string (simpler for quickstart)
connection_string = os.environ.get('AZURE_STORAGE_CONNECTION_STRING')
if connection_string:
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
else:
# Option 2: Using DefaultAzureCredential (recommended for production)
account_url = os.environ.get('AZURE_STORAGE_ACCOUNT_URL', 'https://<your_account_name>.blob.core.windows.net')
if not account_url.startswith('https://') and 'AZURE_STORAGE_ACCOUNT_NAME' in os.environ:
account_name = os.environ['AZURE_STORAGE_ACCOUNT_NAME']
account_url = f'https://{account_name}.blob.core.windows.net'
credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(account_url, credential=credential)
container_name = 'mytestcontainer'
blob_name = 'mytestblob.txt'
local_file_name = 'sample.txt'
data = 'Hello, Azure Blob Storage!'
print(f"Creating container: {container_name}")
container_client = blob_service_client.get_container_client(container_name)
try:
container_client.create_container()
except Exception as e:
if 'ContainerAlreadyExists' not in str(e):
raise
print(f"Container '{container_name}' already exists.")
with open(local_file_name, 'w') as file:
file.write(data)
print(f"Uploading blob: {blob_name}")
with open(local_file_name, 'rb') as data_file:
container_client.upload_blob(name=blob_name, data=data_file, overwrite=True)
print(f"Listing blobs in '{container_name}':")
for blob in container_client.list_blobs():
print(f" - {blob.name}")
print(f"Downloading blob: {blob_name}")
download_blob_client = container_client.get_blob_client(blob_name)
download_data = download_blob_client.download_blob().readall()
print(f"Downloaded content: {download_data.decode('utf-8')}")
# Clean up
# print(f"Deleting blob: {blob_name}")
# container_client.delete_blob(blob_name)
# print(f"Deleting container: {container_name}")
# container_client.delete_container()
except Exception as ex:
print(f"Error: {ex}")
print("Please ensure environment variables (AZURE_STORAGE_CONNECTION_STRING or AZURE_STORAGE_ACCOUNT_URL/NAME and Azure Identity vars) are set correctly.")