I followed the steps in this video (https://www.youtube.com/watch?v=ItE6MAZaiJY) to create a virtual environment and zip the dependencies along with my code.
My code:
import os
import io
import boto3
import botocore
from botocore.exceptions import NoCredentialsError, ClientError
from PIL import Image
s3_resource = boto3.resource('s3')
def get_img_list():
s3 = boto3.client('s3')
response = s3.list_objects_v2(
Bucket='imagesforgreendub',
Prefix='photo_test/',
)
for val in response['Contents']:
if val['Key'] != 'photo_test/':
temp = val['Key']
img_name = temp.split('/')
print(img_name)
if img_name[1] != '':
thumb_name = 'thumb_' + str(img_name[1])
print(thumb_name, img_name[1])
img = image_from_s3('imagesforgreendub', val['Key'])
img_thumb(img, thumb_name)
def image_from_s3(bucket_name, key):
bucket = s3_resource.Bucket(bucket_name)
image = bucket.Object(key)
img_data = image.get().get('Body').read()
return Image.open(io.BytesIO(img_data))
def img_thumb(img, thumb_name):
MAX_SIZE = (100, 100)
img.thumbnail(MAX_SIZE)
img.save('local_img.jpg')
s3 = boto3.client('s3')
s3.upload_file('local_img.jpg', 'imagesforgreendub', '%s/%s' % ('thumbs', thumb_name))
The handler name is lambda_function.get_img_list
(get_img_list() is the first function that should run).
I read on this thread that the local libraries are compiled for the local machine architecture (I am using Windows 10 and Powershell).
The github link given in the aforementioned thread for lambda packages has Pillow for Python 2.7 and I am using 3.8.
How do I resolve this?
Thanks.