-
Notifications
You must be signed in to change notification settings - Fork 3
/
my_app.py
66 lines (52 loc) · 1.84 KB
/
my_app.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
from flask import Flask, request, render_template
from PIL import Image
import io
import base64
import calendar
from datetime import datetime, timedelta
import boto3
s3 = boto3.resource('s3')
app = Flask(__name__)
BUCKET_NAME = 'lmbda'
@app.route('/')
def hello():
return "Hello, world!", 200
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
new_file_b64 = request.form['b64file']
if new_file_b64:
# Decode the image
new_file = base64.b64decode(new_file_b64)
# Crop the Image
img = Image.open(io.BytesIO(new_file))
img.thumbnail((200, 200))
# Tag this filename with an expiry time
future = datetime.utcnow() + timedelta(days=10)
timestamp = str(calendar.timegm(future.timetuple()))
filename = "thumb.%s.jpg" % timestamp
# Send the Bytes to S3
img_bytes = io.BytesIO()
img.save(img_bytes, format='JPEG')
s3_object = s3.Object(BUCKET_NAME, filename)
resp = s3_object.put(
Body=img_bytes.getvalue(),
ContentType='image/jpeg'
)
if resp['ResponseMetadata']['HTTPStatusCode'] == 200:
# Make the result public
object_acl = s3_object.Acl()
response = object_acl.put(
ACL='public-read')
# And return the URL
object_url = "https://{0}.s3.amazonaws.com/{1}".format(
BUCKET_NAME,
filename)
return object_url, 200
else:
return "Something went wrong :(", 400
return render_template('upload.html')
# We only need this for local development.
if __name__ == '__main__':
app.debug = True
app.run()