Quickstart: Create and manage Azure Storage queues using Azure CLI
This quickstart guides you through the process of creating and managing Azure Storage queues using the Azure Command-Line Interface (CLI).
Note: This article assumes you have an Azure subscription. If you don't have one, you can create a free account before you begin.
Prerequisites
- An Azure account.
- Azure CLI installed. If you don't have it installed, see Install the Azure CLI.
- A storage account. If you don't have one, you can create one using the Azure CLI.
1. Sign in to Azure
Sign in to your Azure account using the Azure CLI:
az login
Follow the prompts to authenticate.
2. Create a Storage Account (if you don't have one)
If you already have a storage account, you can skip this step. To create a new storage account, you need a resource group. Let's create one first:
az group create --name MyResourceGroup --location eastus
Now, create the storage account. Replace mystorageaccountcli with a unique name:
az storage account create --name mystorageaccountcli --resource-group MyResourceGroup --location eastus --sku Standard_LRS
3. Get Storage Account Connection String
You'll need the connection string to interact with your storage account. Retrieve it using the Azure CLI:
# Replace 'mystorageaccountcli' with your storage account name
az storage account show-connection-string -n mystorageaccountcli -g MyResourceGroup --query connectionString -o tsv
Copy the output. You'll use this connection string in the following commands. It's recommended to store it in an environment variable for security:
# Example for Linux/macOS
export AZURE_STORAGE_CONNECTION_STRING="YOUR_CONNECTION_STRING"
# Example for Windows Command Prompt
set AZURE_STORAGE_CONNECTION_STRING="YOUR_CONNECTION_STRING"
# Example for Windows PowerShell
$env:AZURE_STORAGE_CONNECTION_STRING="YOUR_CONNECTION_STRING"
Important: Never hardcode connection strings in your application code. Use environment variables or Azure Key Vault.
4. Create a Queue
Use the az storage queue create command to create a new queue. Replace myqueue with your desired queue name:
az storage queue create --name myqueue
If the queue is created successfully, you'll see output like this:
{
"currentMessageCount": 0
}
5. Add Messages to the Queue
Use the az storage message put command to add messages to your queue:
# Add a simple text message
az storage message put --queue-name myqueue --content "Hello Azure Queues!"
# Add another message with a JSON payload
az storage message put --queue-name myqueue --content '{"task": "process_image", "url": "https://example.com/image.jpg"}'
6. View Messages in the Queue
To see the messages in the queue without deleting them, use the az storage message peek command:
az storage message peek --queue-name myqueue
This command will return the messages in the queue, including their message ID, insertion time, expiration time, and the message content.
7. Get and Dequeue Messages
To retrieve and remove messages from the queue (dequeue), use the az storage message get command. This command makes the message invisible for a specified period (visibility timeout) so other consumers don't process it simultaneously.
# Get one message (visibility timeout defaults to 30 seconds)
az storage message get --queue-name myqueue
# Get messages with a custom visibility timeout of 60 seconds
az storage message get --queue-name myqueue --num-messages 5 --timeout 60
Once you've processed a message, you can optionally delete it using its pop receipt. However, az storage message get typically returns the pop receipt, and you'd use that in a separate delete command if needed. For simplicity in this quickstart, the get command removes it after the visibility timeout expires if not explicitly deleted.
8. Delete a Queue
When you no longer need a queue, you can delete it using the az storage queue delete command:
az storage queue delete --name myqueue
Confirm the deletion when prompted.
Next Steps
Congratulations! You've successfully created and managed Azure Storage queues using the Azure CLI.
- Learn more about managing queues with Azure CLI.
- Explore other Azure Storage services like Blob Storage and Table Storage.
- Integrate Azure Queues with other Azure services like Azure Functions or Azure Logic Apps for event-driven architectures.