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
- An Azure subscription. If you don't have one, create a free account before you begin.
- An Azure Storage account. If you don't have one, you can create one using the Azure portal.
Step 1: Create an Azure Storage Account
If you haven't already, create a storage account. You can do this through the Azure portal:
- Sign in to the Azure portal.
- Click on + Create a resource.
- Search for "Storage account" and select it.
- Click Create.
- Fill in the required details, including subscription, resource group, storage account name (globally unique), region, and performance tier.
- 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
- Navigate to your storage account in the Azure portal.
- In the left-hand menu, under Security + networking, select Access keys.
- You will see two access keys and two connection strings. Copy one of the Connection string values.
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:
- Create a new .NET Console Application project.
- Install the
Azure.Storage.QueuesNuGet package. - Replace the placeholder connection string with your actual connection string, or set it as an environment variable named
AZURE_STORAGE_CONNECTION_STRING. - Run the application.
Step 4: Understanding Queue Operations
- Sending Messages: Use
SendMessageAsync()to add a message to the queue. Messages can be up to 64 KB in size. - Retrieving Messages: Use
ReceiveMessagesAsync()to get one or more messages from the queue. When a message is retrieved, it becomes invisible to other consumers for a specified visibility timeout. - Deleting Messages: Once a message has been successfully processed, you must delete it using
DeleteMessageAsync(). If you don't delete it, it will reappear in the queue after the visibility timeout expires. - Peek Messages: Use
PeekMessagesAsync()to retrieve messages without making them invisible. This is useful for inspecting messages before processing.
Next Steps
- Learn more about Azure Queue Storage features.
- Explore Azure Blob Storage for unstructured data.
- Discover Azure Table Storage for NoSQL key-value data.