0

My current Lambda_function code is: (it's purpose is to resize images when to 600x400 when uploaded and then having them saved)

import boto3
import os
import pathlib
from io import BytesIO
from PIL import Image


s3 = boto3.resource('s3')

def delete_this_bucket(name):
    bucket = s3.Bucket(name)
    for key in bucket.objects.all():
        try:
            key.delete()
            bucket.delete()
        except Exception as e:
            print("SOMETHING IS BROKEN !!")

def create_this_bucket(name, location):
    try:
        s3.create_bucket(
            Bucket=name,
            CreateBucketConfiguration={
                'LocationConstraint': location
            }
        )
    except Exception as e:
        print(e)

def upload_test_images(name):
    for each in os.listdir('./testimage'):
        try:
            file = os.path.abspath(each)
            s3.Bucket(name).upload_file(file, each)
        except Exception as e:
            print(e)

def copy_to_other_bucket(src, des, key):
    try:
        copy_source = {
            'Bucket': src,
            'Key': key
        }
        bucket = s3.Bucket(des)
        bucket.copy(copy_source, key)
    except Exception as e:
        print(e)


def resize_image(src_bucket, des_bucket):
    size = 600, 400
    bucket = s3.Bucket(src_bucket)
    in_mem_file = BytesIO()
    client = boto3.client('s3')

    for obj in bucket.objects.all():
        file_byte_string = client.get_object(Bucket=src_bucket, Key=obj.key)['Body'].read()
        im = Image.open(BytesIO(file_byte_string))

        im.thumbnail(size, Image.ANTIALIAS)
        # ISSUE : https://stackoverflow.com/questions/4228530/pil-thumbnail-is-rotating-my-image
        im.save(in_mem_file, format=im.format)
        in_mem_file.seek(0)

        response = client.put_object(
            Body=in_mem_file,
            Bucket=des_bucket,
            Key='resized_' + obj.key
        )

def lambda_handler(event, context):
    bucket = s3.Bucket('bucketname1')

    for obj in bucket.objects.all():
        copy_to_other_bucket(bucket, 'bucketname1', obj.key)

    resize_image(bucket.name, 'bucketname1')


    print(bucket)

And I'm having errors with ssl verification it says.

"errorMessage": "SSL validation failed for https://bucketname1.s3.eu-west-3.amazonaws.com/?encoding-type=url [Errno 2] No such file or directory",
  "errorType": "SSLError",

Can anyone help me with what the problem is? I think it might be either a problem in code or some AWS lambda settings configuration.

1 Answers1

0

You are calling im.save() which saves the image to a local file. You are passing https://bucketname1.s3.eu-west-3.amazonaws.com/?encoding-type=url as the file path to save it to. That is a URL for an object in S3, not a local path that the library can save the file to. So it's giving you that error because it can't figure out how to save a file to that location.

Since the next thing you do is use boto3 to upload the file to S3, and you appear to be trying to keep the image in memory instead of writing it to disk, I'm pretty sure you can just delete the im.save() line. If you do need to call im.save() then you will have to give it a path in the /tmp folder, as that is the only folder in a Lambda execution environment that you have write access to.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • what if i'm using a seconday bucket as a temporary one? – Jerry Papa Oct 23 '20 at 17:33
  • An S3 bucket is not a local file system. It's actually not a file system at all, it's an object store. The `im.save()` method is trying to write to a file system, and doesn't have built-in support to interact with the S3 API. – Mark B Oct 23 '20 at 17:37
  • I've never used Python Image Library before, but like I said in my answer you should be able to simply delete the line were you are trying to save the file. You aren't doing anything with the saved file, you are only using the image contents in memory. Please try deleting that line and come back here if you still have an error. – Mark B Oct 23 '20 at 19:33