Azure

Utilizing Azure Queue Storage with Python

Azure Queue Storage is a straightforward queue storage service in Azure. It’s accountable for asynchronously constructing message queues. The Queue Storage acts as a center layer in a distributed setting and is uncovered utilizing the HTTP protocol. Azure Queue Storage consists of the next elements: storage account, queue, and message. A storage account can have a number of queues, and every queue can have a number of messages.

This article will cowl create a message queue in Azure Queue Storage. Moreover, it should additionally clarify carry out different CRUD operations on the file utilizing the Python programming language. 

 

Stipulations:

Earlier than continuing with this tutorial, it’s endorsed to have an energetic Azure Storage account and Python Three put in in your machine.

 

1. Create the Storage Account:

Earlier than beginning with Queue Storage, you will have the Azure Storage Account. This may be created both by means of the Azure Storage web site, Azure CLI or PowerShell. Head over to Azure Storage web site, and create storage named “demoStorage”. 

 

 

 

2. Get the Connection String:

The second step is to get the connection string in your Storage Account. You possibly can simply get the important thing from the “Entry Key” beneath the “Settings” panel of your Azure Storage Account. Copy the connection string. Subsequent, head over to Python code and set the connection string as proven under: 

 

AZURE_STORAGE_CONNECTION_STRING = “Your_Connection_String” 

 

 

3. Set up the Azure SDK Storage:

Since we’re utilizing Python to carry out CRUD operations on Azure Queue Storage, it’s essential to put in Azure SDK Storage for Python. Use the PIP command to put in the required packages as proven under: pip set up azure-storage-file-share

 

 

4. Create Azure Queue Storage Shopper:

To speak with the Azure Queue Storage, you will have a Queue Shopper. The Queue Shopper requires a queue identify and the Storage Account connection string. Execute the code given under to create the Queue Shopper:

 

from azure.storage.queue import (

        QueueClient,

        BinaryBase64EncodePolicy,

        BinaryBase64DecodePolicy

)

import os, uuid

# Retrieve the connection string from an setting

# variable named AZURE_STORAGE_CONNECTION_STRING

connect_str = os.getenv(“AZURE_STORAGE_CONNECTION_STRING”)

 

# Create a singular identify for the queue

q_name = “queue-” + str(uuid.uuid4())

# Instantiate a QueueClient object 

print(“Creating queue: ” + q_name)

queue_client = QueueClient.from_connection_string(connect_str, q_name)

# Create the queue

queue_client.create_queue()

 

Azure Queue Storage normally shops messages as texts. Nonetheless, to retailer binary knowledge, you’ll be able to set Base64 encoding and decoding capabilities. The perform will run earlier than you set the messages within the queue.

 

# Setup Base64 encoding and decoding capabilities

base64_queue_client = QueueClient.from_connection_string(

                            conn_str=connect_str, queue_name=q_name,

                            message_encode_policy = BinaryBase64EncodePolicy(),

                            message_decode_policy = BinaryBase64DecodePolicy()

                        )

 

5. Add the Message on Azure Queue Storage:

Now that the Azure Queue Shopper is prepared, you’ll be able to simply add the message utilizing the send_message() perform. Execute the code given under to retailer the message within the queue:

 

message = u”Good day Reader. This can be a message.”

print(“Including message: ” + message)

queue_client.send_message(message)

 

6. View the Message Saved on the Azure Queue:

You too can peek on the messages saved within the queue. The peek perform permits customers to have a look at the primary message with out eradicating it from the queue. The pattern code to view the message in Azure Queue is given under:

 

# Peek on the first message

messages = queue_client.peek_messages()

for peeked_message in messages:

    print(“Peeked message: ” + peeked_message.content material)

 

7. Edit the Message Saved on Azure Queue:

Just like different Azure Storage providers, Azure Queue additionally permits the saved content material to be edited. As an example, to vary the contents of the message within the queue, execute the next code:

 

messages = queue_client.receive_messages()

list_result = subsequent(messages)

message = queue_client.update_message(

        list_result.id, list_result.pop_receipt,

        visibility_timeout=0, content material=u’I’m changing the outdated message.’)

print(“Up to date message to: ” + message.content material)

 

8. Dequeue the Messages Saved on the Azure Queue:

After the messages have been processed, it’s important to dequeue the messages saved on the queue. This step is essential in order that the messages aren’t processed once more by mistake. 

messages = queue_client.receive_messages()

for message in messages:

    print(“Dequeuing message: ” + message.content material)

    queue_client.delete_message(message.id, message.pop_receipt)

 

The receive_messages() perform makes the message invisible to the opposite code for 30 seconds. It’s essential to name the delete_message() perform to delete that message instantly. The above methodology deletes one message from the queue. To delete the messages in batch, execute the next code:

messages = queue_client.receive_messages(messages_per_page=5, visibility_timeout=5*60)

for msg_batch in messages.by_page():

   for msg in msg_batch:

      print(“Batch dequeue message: ” + msg.content material)

      queue_client.delete_message(msg)

 

9. Dequeue the Messages Saved on the Azure Queue:

To delete the queue from the Azure Storage account, execute the code following code:

print(“Deleting queue: ” + queue_client.queue_name)

queue_client.delete_queue()

 

Conclusion:

In conclusion, the Azure File Queue Storage permits customers to carry out CRUD operations on messages saved in queues utilizing Python and Azure Storage SDK. Hopefully this information will help you in establishing your Azure Queue Storage and performing CRUD operations utilizing Python efficiently.

Pleased coding pals!

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button