Microsoft Azure Documentation

Getting Started with Azure Queue Storage

Azure Queue Storage is a service that allows you to store large numbers of messages that can be accessed from anywhere in the world via HTTP or HTTPS. A queue is a collection of messages. The primary use of a queue is to store messages that will be processed asynchronously.

This tutorial will guide you through the basic steps of creating an Azure Storage account, creating a queue, and adding and retrieving messages from it.

Prerequisites

Step 1: Create an Azure Storage Account

If you haven't already, create a storage account. You can do this through the Azure portal:

  1. Sign in to the Azure portal.
  2. Click on + Create a resource.
  3. Search for "Storage account" and select it.
  4. Click Create.
  5. Fill in the required details, including subscription, resource group, storage account name (globally unique), region, and performance tier.
  6. Review and create the storage account.

Once created, you'll need your storage account's connection string to interact with it programmatically.

Step 2: Get the Connection String

  1. Navigate to your storage account in the Azure portal.
  2. In the left-hand menu, under Security + networking, select Access keys.
  3. You will see two access keys and two connection strings. Copy one of the Connection string values.
Keep your connection string secure. It provides full access to your storage account.

Step 3: Interact with Queue Storage using a SDK (Example: Azure SDK for .NET)

We'll use the Azure SDK for .NET to demonstrate creating a queue and adding messages. First, ensure you have the necessary NuGet package installed:

Install-Package Azure.Storage.Queues

Here's a C# code example:

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

public class QueueStorageExample
{
    public static async Task Main(string[] args)
    {
        // Replace with your actual connection string
        string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
        string queueName = "my-beginner-queue";

        // Create a QueueClient object
        QueueClient queueClient = new QueueClient(connectionString, queueName);

        // Create the queue if it doesn't exist
        await queueClient.CreateIfNotExistsAsync();
        Console.WriteLine($"Queue '{queueName}' created or already exists.");

        // Send a message to the queue
        string messageText = "Hello, Azure Queue Storage!";
        await queueClient.SendMessageAsync(messageText);
        Console.WriteLine($"Message '{messageText}' sent to the queue.");

        // Retrieve messages from the queue
        Console.WriteLine("Retrieving messages:");
        var response = await queueClient.ReceiveMessagesAsync(maxMessages: 5);

        foreach (var message in response.Value)
        {
            Console.WriteLine($"  Message ID: {message.MessageId}");
            Console.WriteLine($"  Content: {message.Body}");
            Console.WriteLine($"  Dequeued On: {message.InsertedOn}");

            // IMPORTANT: Delete the message after processing
            await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
            Console.WriteLine("  Message deleted.");
        }

        Console.WriteLine("Finished processing queue messages.");
    }
}

To run this code:

  1. Create a new .NET Console Application project.
  2. Install the Azure.Storage.Queues NuGet package.
  3. Replace the placeholder connection string with your actual connection string, or set it as an environment variable named AZURE_STORAGE_CONNECTION_STRING.
  4. Run the application.

Step 4: Understanding Queue Operations

Next Steps