Guest User

Untitled

a guest
May 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. from __future__ import print_function
  2. import boto3
  3. import os
  4. import sys
  5. import uuid
  6. from PIL import Image
  7. import PIL.Image
  8.  
  9. s3_client = boto3.client('s3')
  10.  
  11. def resize_image(image_path, resized_path):
  12. with Image.open(image_path) as image:
  13. image.thumbnail(tuple(x / 2 for x in image.size))
  14. image.save(resized_path)
  15.  
  16. def handler(event, context):
  17. for record in event['Records']:
  18. bucket = record['s3']['bucket']['name']
  19. key = record['s3']['object']['key']
  20. download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
  21. upload_path = '/tmp/resized-{}'.format(key)
  22.  
  23. s3_client.download_file(bucket, key, download_path)
  24. resize_image(download_path, upload_path)
  25. s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)
Add Comment
Please, Sign In to add comment