forked from AppliedTrust/traildash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackfill.py
More file actions
executable file
·51 lines (39 loc) · 1.4 KB
/
backfill.py
File metadata and controls
executable file
·51 lines (39 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
####################
# Neccesary Environment Variables:
# AWS_S3_BUCKET - bucket name to search in
# AWS_SQS_URL - SQS queue to send messages to
# AWS_REGION - AWS region to work in. Must be the same for bucket and sqs
#
# Optional parameter
# <prefix> - pass an optional S3 prefix as first parameter
####################
import json
import sys
from os import environ
import boto3
if not all([environ.get('AWS_S3_BUCKET'), environ.get('AWS_SQS_URL')]):
print('You have to specify the AWS_S3_BUCKET and AWS_SQS_URL environment variables.')
print('Check the "Backfilling data" section of the README file for more info.')
exit(1)
bucket = boto3.resource('s3',region_name=environ.get('AWS_REGION')).Bucket(environ.get('AWS_S3_BUCKET'))
queue = boto3.resource('sqs',region_name=environ.get('AWS_REGION')).Queue(environ.get('AWS_SQS_URL'))
if len(sys.argv) >= 2:
print('S3 prefix ' + sys.argv[1])
items = bucket.objects.filter(Prefix=sys.argv[1])
else:
items = bucket.objects.all()
items_queued = 0
for item in items:
if not item.key.endswith('.json.gz'):
continue
queue.send_message(
MessageBody=json.dumps({
'Message': json.dumps({
's3Bucket': environ.get('AWS_S3_BUCKET'),
's3ObjectKey': [item.key]
})
})
)
items_queued += 1
print('Done! {} items were backfilled'.format(items_queued))