-
Notifications
You must be signed in to change notification settings - Fork 2
/
connect_to_queue.py
73 lines (56 loc) · 2.14 KB
/
connect_to_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import os
from time import sleep
from subprocess import call
import boto
import boto.sqs
def check_queue():
script_principal_id = 'AWS:{0}'.format(get_script_principal_id())
print(script_principal_id)
import sys; sys.exit()
# TODO: load region, queue and bucket from config file
conn = boto.sqs.connect_to_region('eu-west-1')
queue = conn.get_queue('imageResize')
while(True):
messages = queue.get_messages()
print('{0} messages'.format(len(messages)))
for message in messages:
data = json.loads(message.get_body())
if valid(data, script_principal_id):
filename = data['Records'][0]['s3']['object']['key']
print('Compressing {0}'.format(filename))
compress_s3_file(filename)
queue.delete_message(message)
# TODO: get sleep time from config file
sleep(1)
def valid(data, script_principal_id):
# TOOD: check event wasn't a delete here
if 'Records' in data:
principal_id = data['Records'][0]['userIdentity']['principalId']
if principal_id != script_principal_id:
return True
return False
def compress_s3_file(filename):
compressed_filename = 'compressed-{0}'.format(filename)
key = get_key_from_s3(filename)
key.get_contents_to_filename(filename)
compress_file(filename, compressed_filename)
key.set_contents_from_filename(compressed_filename)
os.remove(filename)
os.remove(compressed_filename)
def get_key_from_s3(s3_filename):
conn = boto.s3.connection.S3Connection()
bucket = conn.get_bucket('pxg-image-resizer')
return bucket.get_key(s3_filename)
def compress_file(filename, compressed_filename):
# Got flags from here https://www.progville.com/frontend/optimizing-jpeg-images-mozjpeg/
# TODO: move jpegtran path to config file
call(['/usr/local/Cellar/mozjpeg/3.1/bin/jpegtran',
'-outfile', compressed_filename,
'-optimise', '-copy', 'none',
filename])
def get_script_principal_id():
conn = boto.connect_iam()
return conn.get_user().user_id
if __name__ == "__main__":
check_queue()