-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_photos.py
44 lines (34 loc) · 1.28 KB
/
sync_photos.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
#!/usr/bin/python
import os
from datetime import datetime, timedelta
import boto
import mimetypes
import traceback
from boto.s3.connection import S3Connection, Bucket, Key
BUCKET = 'yaluandmike'
AWS_ACCESS_KEY = 'AKIAJHKYVIJRLI3T3VIQ'
AWS_SECRET_KEY = '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'
IMG_PATH = 'base/img'
def main():
conn = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
for root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__), IMG_PATH)):
for filename in files:
full_path = os.path.join(root, filename)
try:
upload_file(conn, full_path)
except Exception as e:
traceback.print_exc()
def upload_file(conn, full_path):
b = Bucket(conn, BUCKET)
k = Key(b)
k.key = full_path
expires = datetime.utcnow() + timedelta(days=(25 * 365))
expires = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
k.set_metadata("Content-Type", mimetypes.guess_type(full_path)[0])
k.set_metadata("Expires", expires)
k.set_metadata("Cache-Control", "max-age={0}, public".format(25 * 365 * 36400))
k.set_contents_from_filename(full_path)
k.set_acl('public-read')
print "{} -> http://s3.amazonaws.com/yaluandmike/{}".format(full_path, full_path)
if __name__ == '__main__':
main()