Put Message to Azure Storage Queue

This document applies to Azure Storage Queues.

This guide demonstrates how to add messages to an Azure Storage Queue using various SDKs and REST API. Azure Storage Queues provide a robust messaging solution for decoupling applications and components.

Introduction

Adding a message to a queue is a fundamental operation. Messages are typically represented as strings or JSON objects and are stored in the queue until a consumer retrieves and processes them. The maximum message size is 64 KB.

Prerequisites

Using the Azure Storage SDKs

.NET

The following C# code snippet shows how to add a message to a queue using the Azure.Storage.Queues NuGet package.


using Azure.Storage.Queues;
using System;
using System.Threading.Tasks;

public class QueueMessageSender
{
    public static async Task SendMessageAsync(string connectionString, string queueName, string messageContent)
    {
        // Instantiate a client for the specified queue
        QueueClient queueClient = new QueueClient(connectionString, queueName);

        // Create the queue if it doesn't exist
        await queueClient.CreateIfNotExistsAsync();

        // Send a message to the queue
        await queueClient.SendMessageAsync(messageContent);

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

    // Example usage:
    // public static async Task Main(string[] args)
    // {
    //     string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    //     string queueName = "my-message-queue";
    //     string message = "Hello from .NET!";
    //     await SendMessageAsync(connectionString, queueName, message);
    // }
}
            

Python

Here's how to send a message using the Azure Storage Blob client library for Python.


from azure.storage.queue import QueueClient
import os

def send_message_python(connection_string, queue_name, message_content):
    # Instantiate a client for the specified queue
    queue_client = QueueClient.from_connection_string(connection_string, queue_name)

    # Create the queue if it doesn't exist
    queue_client.create_queue()

    # Send a message to the queue
    response = queue_client.send_message(message_content)

    print(f"Message '{message_content}' sent to queue '{queue_name}'. Message ID: {response['messageid']}")

# Example usage:
# connection_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
# queue_name = "my-python-queue"
# message = "Hello from Python!"
# send_message_python(connection_str, queue_name, message)
            

Node.js

Use the Azure Storage Queue client for Node.js to send messages.


const { QueueClient } = require("@azure/storage-queue");

async function sendMessageNodejs(connectionString, queueName, messageContent) {
    // Instantiate a client for the specified queue
    const queueClient = new QueueClient(connectionString, queueName);

    // Create the queue if it doesn't exist
    await queueClient.createIfNotExists();

    // Send a message to the queue
    const response = await queueClient.sendMessage(messageContent);

    console.log(`Message '${messageContent}' sent to queue '${queueName}'. Message ID: ${response.messageId}`);
}

// Example usage:
// const connectionStr = process.env.AZURE_STORAGE_CONNECTION_STRING;
// const queueName = "my-nodejs-queue";
// const message = "Hello from Node.js!";
// sendMessageNodejs(connectionStr, queueName, message);
            

Using the REST API

You can also interact with Azure Storage Queues directly using the REST API. To add a message, use the PUT MESSAGE operation.

Request URL

The URL for the PUT MESSAGE operation is:


PUT https://<storage-account-name>.queue.core.windows.net/<queue-name>/messages?timeout=<timeout>
            

Request Headers

Include the following headers:

Request Body

The request body contains the message content. It should be formatted as XML.


<QueueMessage>
  <MessageText>Your message content here</MessageText>
</QueueMessage>
            

The message text can be a string or a Base64-encoded string.

Language:
Tip: For complex message payloads, consider serializing your data to JSON and then Base64 encoding the JSON string before sending it as the message content.
Warning: Ensure your connection strings and access keys are kept secure. Do not hardcode them directly in production code. Use environment variables or Azure Key Vault.