0
votes

Following this tutorial: https://levelup.gitconnected.com/resize-an-image-in-aws-s3-using-lambda-function-dc386afd4128

The code in question is used to resize an image upon uploading to bucket x and putting the resized one in bucket x-resized.

All works well up until I reach the testing phase, his s3 zip file gets accepted, however the test phase throws the following error:

{
  "errorMessage": "Unable to import module 'CreateThumbnail': No module named 'CreateThumbnail'",
  "errorType": "Runtime.ImportModuleError"
}

I should also mention that I changed his CreateThumbnail script from

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)

to

def handler(event, context):
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key'] 
        download_path = '/{}{}'.format(uuid.uuid4(), key)
        upload_path = '/resized-{}'.format(key)

From what I figure the script "downloads" the original image from a folder "tmp" and uploads it to a folder "tmp" which I don't have in either of my buckets, as I want it to be done in root.

If anyone can tell me why this happens even though the zip file includes all the required libraries, that would be great (and the CreateThumbnail.py). Thanks.

Fixed the import module problem, now this is the problem:

{
  "errorMessage": "Failed to upload /tmp/resized-HappyFace.jpg to flatoro-resized/HappyFace.jpg: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied",
  "errorType": "S3UploadFailedError",
  "stackTrace": [
    "  File \"/var/task/CreateThumbnail.py\", line 24, in handler\n    s3_client.upload_file(upload_path, '{}-resized'.format(bucket), key)\n",
    "  File \"/var/task/boto3/s3/inject.py\", line 131, in upload_file\n    extra_args=ExtraArgs, callback=Callback)\n",
    "  File \"/var/task/boto3/s3/transfer.py\", line 287, in upload_file\n    filename, '/'.join([bucket, key]), e))\n"
  ]
}
1
please be aware that you only can write to /tmp location in lambda. - Arun K
you haven't posted the code that imports CreateThumbnail.py - Arun K
Have you specified the handler in your function to be CreateThumbnail.handler? The zip seems to work (not tested using S3, but passed your error). I used it just now. - Marcin
Yes I have. I know the code itself work, it has something to do with AWS for sure. What do you mean by that? Do I actually need to create a tmp folder? Does my bucket need some special settings? - dantevn5
@dantevn5 Can you paste your complete code also what's the name of you lambda handler. As other folks saying code is working, I suspect there is something you are missing in the code/configuration. - CK__

1 Answers

0
votes

Without looking at the code it is hard to tell but in the error message I can see " s3_client.upload_file(upload_path, '{}-resized'.format(bucket), key)". Here in the source_location( from where the files needs to be copied) instead of download_path (lambda's/tmp/filename) upload_path is written.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_file