Developing Amazon SQS applications – Unix
The boto3
library provides classes for manipulating the SQS queues. We will start by creating the SQS queue using the boto3
library.
Creating an SQS queue
First, create an object of type sqs and then invoke the create_queue()
function to create an SQS queue. The response object is a Python dictionary that can then be used to fetch the URL of the queue.
The following Python program creates a queue and fetches the URL of the newly created queue:
import boto3 # Create SQS client sqs_object = boto3.client('sqs') response = sqs_object.create_queue( QueueName='packtpub_queue', ) queue_url = response['QueueUrl']
Once the queue is created, let's send a message to it.
Sending a message to the queue
Using the SQS object, invoke the send_message()
function and pass the queue URL, delay time, message, and the message attributes to it. The message attributes hold the metadata related to the actual message.
The following code snippet sends a message to the queue...