-
Notifications
You must be signed in to change notification settings - Fork 1
/
consolidate_queue.py
executable file
·53 lines (43 loc) · 1.55 KB
/
consolidate_queue.py
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
52
53
#!/usr/bin/env python
import sys, argparse, time, random, string
import boto.sqs
from boto.s3.key import Key
from djeval import *
# read N json items in from a given queue, and write them to a given
parser = argparse.ArgumentParser(description='Read N items from an SQS queue and write them to a bucket')
parser.add_argument('queue', help='name of the SQS queue to read from')
parser.add_argument('num_items', help='number of items to read')
parser.add_argument('bucket', help='name of the S3 bucket to write to')
parser.add_argument('keyfolder', help='name of the S3 keyfolder (prefix) to use')
args = parser.parse_args()
conn = boto.sqs.connect_to_region("us-east-1")
q = conn.get_queue(args.queue)
msg("%s ITEMS IN QUEUE." % q.count())
ms = []
msg("READING %i ITEMS." % int(args.num_items))
for ix in range(0, int(args.num_items)):
nextmsg = q.read()
if nextmsg is None:
break
ms.append(nextmsg)
if len(ms) > 0:
msg("COMPOSING BIG BLOB.")
blob = "["
for m in ms:
blob = blob + m.get_body() + ", "
blob = blob + "]"
random.seed()
fifty = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(50))
keyname = '%s/%s.json' % (args.keyfolder, fifty)
msg("WRITING ITEMS TO BUCKET %s, KEY %s" % (args.bucket, keyname))
s3conn = boto.connect_s3()
bucket = s3conn.get_bucket(args.bucket)
k = Key(bucket)
k.key = keyname
k.set_contents_from_string(blob)
msg("DELETING ITEMS FROM QUEUE")
for m in ms:
m.delete()
msg("DONE")
else:
msg("NOTHING IN QUEUE, DONE")