Microsoft Azure Client Libraries for Python (Meta-package)
The `azure` package is a meta-package that historically bundled a wide range of Microsoft Azure client and management libraries for Python. As of version 5.0.0, this meta-package is officially deprecated. Users are strongly advised to install individual service-specific packages (e.g., `azure-storage-blob`, `azure-identity`, `azure-mgmt-compute`) based on their needs, rather than the monolithic `azure` package. The individual Azure SDK for Python client libraries follow a continuous release cadence, with updates often monthly for various services, while adhering to common Azure SDK design guidelines for consistency.
Warnings
- breaking The `azure` meta-package (version 5.0.0 and above) is deprecated. Installing it will not provide the latest SDK versions for individual services and is highly discouraged.
- breaking Major architectural changes occurred between older Azure SDKs (often referred to as 'Track 1' or 'v2') and the current generation ('Track 2' or 'v3'). This involved significant changes to package names, class structures, and authentication patterns (e.g., moving from `msrestazure` to `azure-identity`).
- gotcha Authentication should leverage `DefaultAzureCredential` from `azure-identity` for passwordless connections. Using account keys or hardcoding credentials is less secure and not recommended for production.
- gotcha Many Azure client libraries offer both synchronous (default) and asynchronous (`.aio` suffixed packages/modules) APIs. Mixing them or failing to use an async transport can lead to errors or suboptimal performance.
- gotcha When using Azure management clients (`azure-mgmt-*`), setting a field to `None` in an update (PUT) operation might interpret `None` as a request to *delete* that field, rather than keeping its existing value.
- breaking The `azure-core` library (a foundational dependency for most Azure SDKs) changed its continuation token format in version 1.38.0. This can break pagination and long-running operations if tokens generated by older versions are used with newer clients or vice-versa.
Install
-
pip install azure==4.0.0 -
pip install azure-storage-blob azure-identity
Imports
- BlobServiceClient
from azure.storage.blob import BlobServiceClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
# Replace with your storage account name or retrieve from environment variables
STORAGE_ACCOUNT_NAME = os.environ.get('AZURE_STORAGE_ACCOUNT_NAME', 'yourstorageaccountname')
CONTAINER_NAME = 'my-container'
BLOB_NAME = 'hello_world.txt'
try:
# Authenticate using DefaultAzureCredential (recommended passwordless approach)
credential = DefaultAzureCredential()
# Construct the blob service client
blob_service_client = BlobServiceClient(account_url=f"https://{STORAGE_ACCOUNT_NAME}.blob.core.windows.net/", credential=credential)
# Get a client to interact with a specific container
container_client = blob_service_client.get_container_client(CONTAINER_NAME)
# Create the container if it doesn't exist
try:
container_client.create_container()
print(f"Container '{CONTAINER_NAME}' created.")
except Exception as e:
if "ContainerAlreadyExists" in str(e): # Specific exception for idempotent create
print(f"Container '{CONTAINER_NAME}' already exists.")
else:
raise
# Upload data to a blob
data = "Hello, Azure Blob Storage! This is a test blob."
blob_client = container_client.get_blob_client(BLOB_NAME)
blob_client.upload_blob(data, overwrite=True)
print(f"Blob '{BLOB_NAME}' uploaded to container '{CONTAINER_NAME}'.")
# Download the blob data
downloaded_blob = blob_client.download_blob().readall()
print(f"Downloaded blob content: {downloaded_blob.decode('utf-8')}")
# Clean up (optional: delete the blob and container)
# blob_client.delete_blob()
# print(f"Blob '{BLOB_NAME}' deleted.")
# container_client.delete_container()
# print(f"Container '{CONTAINER_NAME}' deleted.")
except Exception as ex:
print(f"An error occurred: {ex}")