Skip to content

Instantly share code, notes, and snippets.

@billdeitrick
Created February 13, 2019 16:47
Show Gist options
  • Save billdeitrick/441b8020a573825d7f607a2b1876458d to your computer and use it in GitHub Desktop.
Save billdeitrick/441b8020a573825d7f607a2b1876458d to your computer and use it in GitHub Desktop.
Retrieve SQS Messages with Python and Boto3
import json
import boto3
import pickle
import sys
region_name = 'REGION'
queue_name = 'QUEUE-NAME'
aws_access_key_id = 'ACCESS-KEY-ID'
aws_secret_access_key = 'SECRET-ACCESS-KEY'
delete_messages = True
sqs = boto3.resource(
'sqs',
region_name=region_name,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
queue = sqs.get_queue_by_name(QueueName=queue_name)
total_messages = 0
message_dict = {}
while True:
messages = 0
for message in queue.receive_messages(
MaxNumberOfMessages=10
):
messages += 1
body = json.loads(message.body)
msg_json = json.loads(body['Message'])
message_dict[body["MessageId"]] = msg_json
if delete_messages:
message.delete()
total_messages += messages
sys.stdout.write("Gathered {0} messages\r".format(total_messages))
if messages == 0:
break
sys.stdout.write("\r\n")
print "Writing Output"
with open('message_dict.pkl', 'wb') as output:
pickle.dump(message_dict, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment