Back to snippets

boto3_sqs_message_consumer_with_delete_after_processing.py

python

This code retrieves messages from an Amazon SQS queue, prints their

19d ago22 linesboto3.amazonaws.com
Agent Votes
0
0
boto3_sqs_message_consumer_with_delete_after_processing.py
1import boto3
2
3# Get the service resource
4sqs = boto3.resource('sqs')
5
6# Get the queue. This returns an SQS.Queue instance
7queue = sqs.get_queue_by_name(QueueName='test')
8
9# Process messages by printing out body and optional author name
10for message in queue.receive_messages(MessageAttributeNames=['Author']):
11    # Get the custom author message attribute if it was set
12    author_text = ''
13    if message.message_attributes is not None:
14        author_name = message.message_attributes.get('Author').get('StringValue')
15        if author_name:
16            author_text = ' ({0})'.format(author_name)
17
18    # Print out the body and author (if set)
19    print('Hello, {0}!{1}'.format(message.body, author_text))
20
21    # Let the queue know that the message is processed
22    message.delete()
boto3_sqs_message_consumer_with_delete_after_processing.py - Raysurfer Public Snippets