From 1af9ba8e3d2e0de757d0ad701402e0adb40d6ffb Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Mon, 12 Nov 2018 20:33:19 -0500 Subject: [PATCH] adding photoflow.py --- photoflow.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 photoflow.py diff --git a/photoflow.py b/photoflow.py new file mode 100644 index 0000000..ed2d3b4 --- /dev/null +++ b/photoflow.py @@ -0,0 +1,25 @@ +from __future__ import print_function +import boto3 +import os +import sys +import uuid +from PIL import Image +import PIL.Image + +s3_client = boto3.client('s3') + +def resize_image(image_path, resized_path): + with Image.open(image_path) as image: + image.thumbnail(tuple(x / 2 for x in image.size)) + image.save(resized_path) + +def handler(event, context): + for record in event['Records']: + bucket = record['s3']['bucket']['name'] + key = record['s3']['object']['key'] + download_path='/tmp/{}{}'.format(uuid.uuid4(), key) + upload_path='/tmp/resized-{}'.format(key) + + s3_client.download_file(bucket, key, download_path) + resize_image(download_path, upload_path) + s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)