Azure Queue Storage Client Library for Python

raw JSON →
12.15.0 verified Tue May 12 auth: no python install: verified quickstart: verified

The Azure Queue Storage client library for Python (azure-storage-queue) enables Python applications to interact with Azure Queue Storage, a service for storing large numbers of messages. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, facilitating asynchronous work backlogs and decoupled distributed applications. The library is actively maintained as part of the Azure SDK for Python, with version 12.15.0 being the latest stable release.

pip install azure-storage-queue azure-identity
error Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
cause This error typically indicates an issue with the credentials used to access the Azure Queue Storage, such as an incorrect account name, account key, Shared Access Signature (SAS) token, or an expired SAS token.
fix
Verify that the storage account name and account key are correct and up-to-date. If using a SAS token, ensure it is valid, has the necessary permissions (read, write, etc.), and has not expired. Also, check that your system's clock is synchronized, as a significant time difference can cause authentication failures.
error ModuleNotFoundError: No module named 'azure.storage'
cause This error occurs when an application tries to import from the deprecated `azure.storage` meta-package instead of the service-specific `azure-storage-queue` package, or if the `azure-storage-queue` package is not correctly installed.
fix
First, uninstall any old azure-storage packages: pip uninstall azure-storage. Then, ensure you have the correct, service-specific client library installed: pip install azure-storage-queue. Update your import statements to from azure.storage.queue import QueueServiceClient or QueueClient.
error QueueNotFound
cause This error indicates that the queue you are trying to access or perform an operation on does not exist in the specified storage account, or the credentials used do not have permission to list/access it.
fix
Verify that the queue name is spelled correctly and exists in your Azure Storage account. If it's a new queue, ensure you call create_queue() or create_queue_if_not_exists() on the QueueClient or QueueServiceClient before attempting other operations. Also, check that the credentials (account key, SAS token, or Azure AD principal) have the necessary 'read' and 'process' permissions for queues.
error MessageTooLarge
cause This error occurs when you attempt to send a message to an Azure Queue that exceeds the maximum allowed size, which is 64 KB.
fix
Ensure that the content of your message, after Base64 encoding (if applicable), does not exceed 64 KB. If your data is larger, you should consider splitting it into multiple smaller messages or storing the larger data in Azure Blob Storage and sending a reference (e.g., a Blob URL) to that data in the queue message.
error Connection string format is incorrect
cause This error arises when the connection string provided to initialize the `QueueServiceClient` or `QueueClient` is malformed, missing required parameters, or contains incorrect values.
fix
Ensure your connection string strictly adheres to the format: DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net. Double-check for typos, missing semicolons, or incorrect casing. Retrieve the exact connection string from the Azure portal for accuracy.
breaking Major architectural changes occurred with the release of the v12 SDK. Older client libraries (e.g., `azure-storage` or `Microsoft.Azure.Storage.Queue`) are deprecated and no longer maintained. Applications must migrate to the `azure-storage-queue` package, which involves new client constructors, model names, and authentication patterns.
fix Rewrite client initialization and interactions using the `azure-storage-queue` library. Prioritize passwordless authentication with `DefaultAzureCredential` over shared keys. Refer to the official migration guides.
gotcha Messages sent to and received from Azure Queue Storage are often base64 encoded by default, especially when dealing with non-string content like JSON or binary data. Failing to explicitly encode before sending or decode after receiving can lead to corrupted data or deserialization errors.
fix Always use `BinaryBase64EncodePolicy()` and `BinaryBase64DecodePolicy()` when creating `QueueServiceClient` or `QueueClient` if you intend to send/receive non-string data that requires base64 encoding/decoding. For JSON messages, ensure you explicitly `json.dumps()` before sending and `json.loads()` after decoding.
gotcha Azure Queue Storage lacks a built-in dead-letter queue mechanism, unlike Azure Service Bus. If a message consistently fails processing, it will reappear in the queue after its visibility timeout expires. This can lead to a 'poison message' blocking workers indefinitely and requiring manual intervention.
fix Implement custom dead-lettering logic by tracking retry counts. If a message exceeds a configured retry threshold, move it to a separate 'poison message' queue or log it for manual inspection and removal from the main queue.
gotcha Using shared access keys or connection strings directly in application code is discouraged, especially in production. This poses a security risk and complicates key rotation.
fix Migrate to passwordless authentication using Azure Identity. Leverage `DefaultAzureCredential` with Managed Identities for Azure-hosted applications or environment variables for local development. Ensure appropriate Azure RBAC roles (e.g., 'Storage Queue Data Contributor') are assigned.
gotcha Incorrectly managing message visibility timeouts can lead to messages reappearing in the queue before processing is complete (leading to reprocessing) or being lost if the application crashes and the message is not explicitly deleted within the timeout.
fix Adjust the `visibility_timeout` parameter when receiving messages to allow sufficient time for processing. If processing takes longer than expected, extend the visibility timeout using `update_message`. Ensure messages are deleted after successful processing.
breaking The application failed to connect to Azure Queue Storage because no authentication details were provided. A connection string (`AZURE_STORAGE_CONNECTION_STRING`), an account URL (`AZURE_STORAGE_ACCOUNT_URL`), or Azure Identity credentials (e.g., via `DefaultAzureCredential` and related environment variables) are required to establish a connection.
fix Ensure that the necessary environment variables (`AZURE_STORAGE_CONNECTION_STRING` or `AZURE_STORAGE_ACCOUNT_URL`, or relevant Azure Identity variables like `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET` for `DefaultAzureCredential`) are correctly set in the application's environment before client initialization. For production, consider using Managed Identities with `DefaultAzureCredential` for enhanced security.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 1.08s 43.8M
3.10 alpine (musl) - - 1.21s 42.7M
3.10 slim (glibc) wheel 3.8s 0.79s 44M
3.10 slim (glibc) - - 0.83s 43M
3.11 alpine (musl) wheel - 1.26s 47.3M
3.11 alpine (musl) - - 1.44s 46.2M
3.11 slim (glibc) wheel 3.7s 1.06s 48M
3.11 slim (glibc) - - 1.08s 47M
3.12 alpine (musl) wheel - 1.46s 38.8M
3.12 alpine (musl) - - 1.45s 37.7M
3.12 slim (glibc) wheel 3.3s 1.38s 39M
3.12 slim (glibc) - - 1.42s 38M
3.13 alpine (musl) wheel - 1.45s 38.6M
3.13 alpine (musl) - - 1.43s 37.3M
3.13 slim (glibc) wheel 3.3s 1.33s 39M
3.13 slim (glibc) - - 1.58s 38M
3.9 alpine (musl) wheel - 0.97s 43.8M
3.9 alpine (musl) - - 1.00s 42.8M
3.9 slim (glibc) wheel 4.5s 1.00s 44M
3.9 slim (glibc) - - 0.99s 43M

This quickstart demonstrates how to create a `QueueServiceClient` (using either a connection string or `DefaultAzureCredential`), create a queue, send messages, peek at messages, receive and delete messages, and finally delete the queue. It highlights the use of `BinaryBase64EncodePolicy` and `BinaryBase64DecodePolicy` for message handling. Ensure `AZURE_STORAGE_CONNECTION_STRING` or `AZURE_STORAGE_ACCOUNT_URL` and necessary Azure Identity environment variables (e.g., `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) are set for authentication.

import os, uuid
from azure.identity import DefaultAzureCredential
from azure.storage.queue import QueueServiceClient, QueueClient, BinaryBase64EncodePolicy, BinaryBase64DecodePolicy

try:
    print('Azure Queue storage - Python quickstart sample')

    # Retrieve the connection string for use with the application.
    # The storage connection string is a key for accessing your storage account.
    # It is recommended to use passwordless authentication in production.
    connect_str = os.environ.get('AZURE_STORAGE_CONNECTION_STRING', '')
    queue_name = "quickstart-" + str(uuid.uuid4())

    # Create the QueueServiceClient object
    if connect_str:
        # Authenticate with connection string
        queue_service_client = QueueServiceClient.from_connection_string(
            connect_str, 
            message_encode_policy=BinaryBase64EncodePolicy(),
            message_decode_policy=BinaryBase64DecodePolicy()
        )
        print("Using connection string for authentication.")
    else:
        # Authenticate with DefaultAzureCredential
        account_url = os.environ.get('AZURE_STORAGE_ACCOUNT_URL') # e.g., 'https://<account-name>.queue.core.windows.net'
        if not account_url:
            raise ValueError("Please set AZURE_STORAGE_CONNECTION_STRING or AZURE_STORAGE_ACCOUNT_URL environment variable.")

        credential = DefaultAzureCredential()
        queue_service_client = QueueServiceClient(
            account_url=account_url, 
            credential=credential,
            message_encode_policy=BinaryBase64EncodePolicy(),
            message_decode_policy=BinaryBase64DecodePolicy()
        )
        print("Using DefaultAzureCredential for authentication.")

    # Get a client to interact with the queue
    queue_client = queue_service_client.get_queue_client(queue_name)

    # Create a queue
    print(f"Creating queue: {queue_name}")
    queue_client.create_queue()

    # Send a message
    message1 = "Hello, Azure Queue!"
    print(f"Adding message: {message1}")
    queue_client.send_message(message1)

    message2 = "This is a second message."
    print(f"Adding message: {message2}")
    queue_client.send_message(message2)

    # Peek at messages
    print("Peeking at messages...")
    peeked_messages = queue_client.peek_messages(max_messages=5)
    for peeked_message in peeked_messages:
        print(f"Peeked message: {peeked_message.content}")

    # Receive and delete messages
    print("Receiving and deleting messages...")
    messages = queue_client.receive_messages(messages_per_page=1)
    for message in messages:
        print(f"Received message: {message.content}")
        queue_client.delete_message(message)
        print(f"Deleted message: {message.content}")

    print(f"Successfully completed the quickstart. Deleting queue: {queue_name}")
    queue_client.delete_queue()

except Exception as ex:
    print(f"Exception: {ex}")