In this post I’ll continue my series on the Azure CLI with a look at how you can manage storage queues and messages. Previous posts in the series are here:
- Manage Blob Storage with the Azure CLI
- Create and Configure a VM with the Azure CLI
- Automate away your Azure Portal usage with the Azure CLI
- Using Queries with the Azure CLI
- Introducing the Azure CLI
I’m going to assume that we’ve already created a storage account and put the connection string into an environment variable called
AZURE_STORAGE_CONNECTION_STRING to save us passing the
--connection-string argument to each command. Read my previous post for more details on how to do this.
To check if a queue exists:
$queueName = "myqueue" az storage queue exists -n $queueName
And to actually create it (safe to call if the queue already exists):
az storage queue create -n $queueName
Posting Messages to a Queue
Now normally you wouldn’t be actually posting messages to queues from the CLI, but it might be useful for testing purposes or to trigger some kind of maintenance task. Here’s how you can post a message to a queue:
az storage message put --content "Hello from CLI" -q $queueName
You can also supply a time to live duration with the
--time-to-live argument which should be specified in seconds.
Retrieving Messages from a Queue
Receiving messages from the queue is a two-step process. First you use
az storage message get to mark a message on the queue as being locked for a set period of time (the “visibility timeout”). You then process the message and call
az storage message delete to delete the message from the queue. If you need more time, you can use
az storage message update to increase the visibility timeout for your message. If you fail to call either
update before the visibility timeout expires, your message becomes visible again for someone else to receive.
Here’s how we can get a message from the queue with a two minute visibility timeout:
az storage message get -q $queueName --visibility-timeout 120
The output from the
get command includes the id and **pop receipt **of the message. These are important as they need to be used in the call to delete (or update). Here’s how we can delete the message for a specific message:
az storage message delete --id "2a1d4311-c952-4199-94ac-801930da31c7" \ --pop-receipt "AgAAAAMAAAAAAAAAuSuJ51RD0wE=" -q $queueName
Again, it’s unlikely you’d often need to write code to read messages from queues from the command line, but it might be useful for diagnostic purposes if you wanted to write a script to resubmit certain messages from a poison message queue for example.