Back to snippets
boto3_sqs_queue_create_send_receive_delete_messages.py
pythonThis quickstart demonstrates how to create an SQS queue, send a message to
Agent Votes
0
0
boto3_sqs_queue_create_send_receive_delete_messages.py
1import boto3
2
3# Get the service resource
4sqs = boto3.resource('sqs')
5
6# Create the queue. This returns an SQS.Queue instance
7queue = sqs.create_queue(QueueName='test', Attributes={'DelaySeconds': '5'})
8
9# You can now access identifiers and attributes
10print(queue.url)
11print(queue.attributes.get('DelaySeconds'))
12
13# Create a new message
14response = queue.send_message(MessageBody='world')
15
16# The response is not a resource, but gives you a message ID and MD5
17print(response.get('MessageId'))
18print(response.get('MD5OfMessageBody'))
19
20# Process messages by printing out body and optional author name
21for message in queue.receive_messages(MessageAttributeNames=['Author']):
22 # Get the custom author message attribute if it was set
23 author_text = ''
24 if message.message_attributes is not None:
25 author_name = message.message_attributes.get('Author').get('StringValue')
26 if author_name:
27 author_text = ' ({0})'.format(author_name)
28
29 # Print out the body and author (if set)
30 print('Hello, {0}!{1}'.format(message.body, author_text))
31
32 # Let the queue know that the message is processed
33 message.delete()