Azure Queue Storage Client Library for Python
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.
Warnings
- 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.
- 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.
- 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.
- 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.
- 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.
Install
-
pip install azure-storage-queue azure-identity
Imports
- QueueServiceClient
from azure.storage.queue import QueueServiceClient
- QueueClient
from azure.storage.queue import QueueClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
- BinaryBase64EncodePolicy
from azure.storage.queue import BinaryBase64EncodePolicy
- BinaryBase64DecodePolicy
from azure.storage.queue import BinaryBase64DecodePolicy
Quickstart
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}")