Skip to content

Instantly share code, notes, and snippets.

@ADelRosarioH
Last active July 27, 2021 02:50
Show Gist options
  • Save ADelRosarioH/a17a584e50bc5519497f8355c192905d to your computer and use it in GitHub Desktop.
Save ADelRosarioH/a17a584e50bc5519497f8355c192905d to your computer and use it in GitHub Desktop.
List AWS S3 objects and enqueue them into an AWS SQS
import sys
import json
import argparse
import boto3
parser = argparse.ArgumentParser("enqueue S3 objects into SQS")
parser.add_argument('bucket_name')
parser.add_argument('prefix')
parser.add_argument('postfix')
parser.add_argument('queue_name')
s3 = boto3.resource('s3')
sqs = boto3.resource('sqs')
def enqueue(bucket_name, prefix, postfix, queue_name):
print('listing for {} in {} to enqueue in {}'.format(prefix, bucket_name, queue_name))
bucket = s3.Bucket(bucket_name)
queue = sqs.get_queue_by_name(QueueName=queue_name)
objects = bucket.objects.filter(Prefix=prefix)
file_count = 0
for obj in objects:
if postfix and not obj.key.endswith(postfix):
continue
file_count += 1
print('adding {} to queue.'.format(obj.key))
message_body = {}
message_body['Records'] = [
{
's3': {
'bucket': {
'name': bucket_name
},
'object': {
'key': obj.key
}
}
}
]
message_body_json = json.dumps(message_body, indent=4)
response = queue.send_message(MessageBody=message_body_json)
print(response.get('MessageId'))
print('')
print('{} objects found.'.format(file_count))
if __name__ == '__main__':
args = parser.parse_args()
enqueue(args.bucket_name, args.prefix, args.postfix, args.queue_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment