WHAT I WANT TO DO
- I have 3 buckets. I want to trigger a lambda function when an image uploaded to bucket1.
- The lambda function will resize that image (500x500) and save that resized image in bucket2.
- the main image on bucket1 will be backed up in bucket3.
- the main image on bucket1 will be deleted then.
WHAT I HAVE DONE SO FAR
wrote a lambda function that will move images across the bucket.
Made S3 trigger to the lambda function
WHERE IS MY PROBLEM
I am using PIL to resize the image. But PIL module is not in python STL. So I zipped my code with site packages and able to run. But the error says file not found. an example of a file key image/myimage.jpg
Though I followed tutorials but if I try to use lambda layers instead of zipping everytime it seems not to find the PIL module.
CODE
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((500, 500))
image.save(resized_path)
def lambda_handler(event, context):
#
# giving a key error here event['Records']
#
for record in event['Records']:
bucket = 'mahabubelahibucket1'
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, 'mahabubelahibucket2', key)