Azure Storage Queues Operations

This document outlines the common operations performed on Azure Storage Queues, providing details on how to interact with queues using various SDKs and REST APIs.

Key Queue Operations

1. Creating a Queue

Queues are created within an Azure Storage account. You can create a queue using the Azure portal, Azure CLI, PowerShell, or client libraries.

Note: Queue names must be valid DNS names, consisting of lowercase letters, numbers, and hyphens. They must start and end with a letter or number, and cannot contain consecutive hyphens. The maximum length for a queue name is 63 characters.

Example using Azure SDK for Python:

from azure.storage.queue import QueueServiceClient

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
queue_name = "my-sample-queue"

queue_service_client = QueueServiceClient.from_connection_string(connection_string)
queue_client = queue_service_client.create_queue(queue_name)

print(f"Queue '{queue_name}' created successfully.")

2. Sending a Message

Messages are added to a queue using the enqueue operation. The message content can be plain text or JSON.

// Example using Azure SDK for .NET
using Azure.Storage.Queues;

string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string queueName = "my-sample-queue";

// Get the queue client
QueueClient queueClient = new QueueClient(connectionString, queueName);

// Send a message
string messageText = "This is the first message.";
queueClient.SendMessage(messageText);

Console.WriteLine($"Message '{messageText}' sent to queue '{queueName}'.");

3. Receiving Messages

Messages can be retrieved from the front of the queue. When you receive a message, it becomes invisible for a specified visibility timeout period. If the message is not deleted within this timeout, it becomes visible again and can be retrieved by other clients.

You can peek at messages without making them invisible, or dequeue them to process and eventually delete.

// Example using Azure SDK for JavaScript
const { QueueClient } = require("@azure/storage-queue");

async function receiveMessage() {
    const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    const queueName = "my-sample-queue";

    const queueClient = QueueClient.fromConnectionString(connectionString, queueName);

    // Receive a message
    const response = await queueClient.receiveMessage();

    if (response.receivedMessage) {
        console.log("Message received:", response.receivedMessage.messageText);
        console.log("Message ID:", response.receivedMessage.messageId);
        console.log("Pop receipt:", response.receivedMessage.popReceipt);

        // Important: Delete the message after processing to remove it permanently
        // await queueClient.deleteMessage(response.receivedMessage.messageId, response.receivedMessage.popReceipt);
        // console.log("Message deleted.");
    } else {
        console.log("No messages in the queue.");
    }
}

receiveMessage();

4. Deleting Messages

Once a message has been successfully processed, it should be deleted from the queue to prevent reprocessing. This requires the message's messageId and popReceipt.

from azure.storage.queue import QueueServiceClient

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
queue_name = "my-sample-queue"

queue_service_client = QueueServiceClient.from_connection_string(connection_string)
queue_client = queue_service_client.get_queue_client(queue_name)

# Assuming you have received a message and have its details
# message = queue_client.receive_message()
# if message:
#     message_id = message.id
#     pop_receipt = message.pop_receipt
#     queue_client.delete_message(message_id, pop_receipt)
#     print(f"Message {message_id} deleted.")

print("Delete message operation demonstrated.")

5. Clearing a Queue

The clear_messages operation removes all messages from the queue.

// Example using Azure SDK for Java
import com.azure.storage.queue.QueueClient;
import com.azure.storage.queue.QueueServiceClient;
import com.azure.storage.queue.QueueServiceClientBuilder;

public class QueueOperations {
    public static void main(String[] args) {
        String connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
        String queueName = "my-sample-queue";

        QueueServiceClient queueServiceClient = new QueueServiceClientBuilder()
            .connectionString(connectionString)
            .buildClient();

        QueueClient queueClient = queueServiceClient.getQueueClient(queueName);

        // Clear all messages from the queue
        queueClient.clearMessages();

        System.out.println("All messages cleared from queue '" + queueName + "'.");
    }
}

6. Deleting a Queue

A queue can be deleted entirely using the delete_queue operation.

# Example using Azure CLI
az storage queue delete --name my-sample-queue --account-name yourstorageaccountname --account-key "YOUR_ACCOUNT_KEY"

Queue Properties and Metadata

You can also retrieve and update queue properties, such as the approximate message count and metadata.

Operation Description SDK Example (Concept)
Get Properties Retrieves the number of messages waiting to be dequeued and the approximate total number of messages in the queue. queue_client.get_properties()
Set Metadata Sets custom metadata for a queue. queue_client.set_metadata(...)
Get Access Policy Retrieves the signed identifiers for the queue. queue_client.get_access_policy()
Set Access Policy Sets the signed identifiers for the queue. queue_client.set_access_policy(...)

Best Practice: When receiving messages, always set an appropriate visibility_timeout to prevent other consumers from picking up the same message while it's being processed. Ensure your application reliably deletes messages after successful processing.