Azure Storage Common Client Library (Track 1 SDK)
The `azure-storage-common` library provides common functionalities and utilities shared across the older Microsoft Azure Storage 'Track 1' client libraries for Python (e.g., `azure-storage-blob` v2.x, `azure-storage-queue` v2.x). It includes classes for managing storage accounts, shared access signatures, and handling exceptions. The current version is 2.1.0, with releases historically tied to new Azure Storage REST API versions, though it is now largely in maintenance mode.
Common errors
-
ModuleNotFoundError: No module named 'azure.common'
cause This error typically occurs when the `azure-common` package or the older 'Track 1' Azure SDK packages (like `azure-storage-blob` v2.x) that depend on it are not installed, or when there's a conflict with newer 'Track 2' Azure SDK packages which have a different module structure.fixEnsure `azure-common` and the specific `azure-storage-*` v2.x packages you intend to use are installed: `pip install azure-common azure-storage-blob==2.1.0` (adjust version as needed). If you intend to use the newer 'Track 2' SDK, uninstall older packages and install the appropriate v12.x packages, e.g., `pip uninstall azure-common azure-storage-blob` then `pip install azure-storage-blob==12.11.0` or later. -
ImportError: cannot import name 'BlockBlobService' from 'azure.storage.blob'
cause This error arises when code written for the older `azure-storage-blob` v2.x library, which used `BlockBlobService`, attempts to run with the newer `azure-storage-blob` v12.x (or later) installed. The v12.x library introduces new client classes like `BlobServiceClient`, `ContainerClient`, and `BlobClient`, deprecating `BlockBlobService`.fixIf you need to use the older `BlockBlobService`, explicitly install version 2.1.0 of `azure-storage-blob`: `pip install azure-storage-blob==2.1.0`. If you intend to use the modern SDK, update your code to use `BlobServiceClient` and other v12.x classes: `from azure.storage.blob import BlobServiceClient`. -
AttributeError: module 'azure.storage' has no attribute 'BlobService'
cause This error occurs when attempting to access an outdated class or attribute (`BlobService` in this case) from an `azure.storage` namespace, usually due to a mix of old and new Azure SDK packages being installed, or trying to use 'Track 1' code with a 'Track 2' installation. The structure and class names changed significantly in the v12 SDK.fixVerify your installed Azure Storage packages (`pip list | grep azure-storage`). If you have both old (e.g., `azure-storage` or `azure-storage-blob` v2.x) and new (`azure-storage-blob` v12.x) packages, uninstall both and reinstall only the version you intend to use. If targeting v12.x, update your code to use the new client objects such as `BlobServiceClient`, `ContainerClient`, or `BlobClient`. -
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. (403 Forbidden)
cause This common authentication failure often indicates problems with the Shared Access Signature (SAS) token, such as it being expired, having incorrect permissions, or being malformed. It can also be due to an incorrect storage account key or an inactive/deleted storage account.fixDouble-check the validity and permissions of your SAS token, including its start and expiration times, and the services/resource types/permissions it grants. Regenerate the SAS token if necessary, ensuring it's correctly applied to the URL. Verify the storage account name and access keys are correct and active. For more secure authentication, consider using Azure Identity with managed identities or service principals.
Warnings
- breaking This library (`azure-storage-common` v2.x) is part of the older 'Track 1' Azure SDK for Python. The newer, recommended 'Track 2' SDK (e.g., `azure-storage-blob` v12.x+, `azure-storage-queue` v12.x+) does NOT use `azure-storage-common`. Migrating from Track 1 to Track 2 requires substantial code changes and a complete re-import strategy.
- deprecated `azure-storage-common` is in maintenance mode for the Track 1 SDK. No new features are being added; only critical bug fixes are provided. For new development, it is highly recommended to use the Track 2 SDKs for Azure Storage.
- gotcha Mixing Track 1 (`azure-storage-common` and its dependent v2.x libraries) with Track 2 (e.g., `azure-storage-blob` v12.x+) within the same application is highly discouraged and can lead to dependency conflicts, unexpected behavior, and increased bundle size.
- gotcha The connection string format expected by `CloudStorageAccount.create_from_connection_string` is specific, requiring keys like `AccountName`, `AccountKey`, `DefaultEndpointsProtocol`, and `EndpointSuffix`. Incorrectly formatted strings will raise a `ValueError`.
- gotcha The library requires the Azure Storage connection string to be set, typically via the `AZURE_STORAGE_CONNECTION_STRING` environment variable, for operations that connect to Azure Storage. Failure to set this variable will prevent successful initialization or operation.
- gotcha The `azure-storage-common` library, particularly when initializing `CloudStorageAccount` without explicit parameters, expects the `AZURE_STORAGE_CONNECTION_STRING` environment variable to be set. Failure to provide this will prevent initialization and raise an error.
Install
-
pip install azure-storage-common
Imports
- CloudStorageAccount
from azure.storage.blob import CloudStorageAccount
from azure.storage.common import CloudStorageAccount
Quickstart
import os
from azure.storage.common import CloudStorageAccount
# Retrieve the connection string from an environment variable.
# Example: AZURE_STORAGE_CONNECTION_STRING="DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey;EndpointSuffix=core.windows.net"
connection_string = os.environ.get("AZURE_STORAGE_CONNECTION_STRING", "")
if not connection_string:
print("Please set the AZURE_STORAGE_CONNECTION_STRING environment variable.")
else:
try:
# Create a CloudStorageAccount object from the connection string
# This object is then used to create specific service clients (e.g., blob, queue, file).
account = CloudStorageAccount.create_from_connection_string(connection_string)
print(f"Successfully created CloudStorageAccount for: {account.account_name}")
print("This library provides common utilities for the Azure Storage Track 1 SDK (v2.x).")
print("To perform storage operations, you would typically use this 'account' object")
print("to instantiate a service client from another Track 1 library (e.g., azure-storage-blob v2.x).")
# Example of how you would use it with azure-storage-blob v2.x (if installed):
# from azure.storage.blob import BlockBlobService
# block_blob_service = account.create_block_blob_service()
# print(f"BlockBlobService created successfully using account: {block_blob_service.account_name}")
except ValueError as e:
print(f"Error creating storage account: {e}")
print("Please ensure the AZURE_STORAGE_CONNECTION_STRING format is correct.")
except Exception as e:
print(f"An unexpected error occurred: {e}")