简体   繁体   中英

Create Thumbnails on Amazon S3 Bucket with Lambda function

I have two buckets on S3. campaignfotos and campaignfotosresized.

在此输入图像描述

The Permissions for those two looks like this:

在此输入图像描述

Created the ThumbnailCreatorRole ( I think I'm missing here something): 在此输入图像描述

I have created a Lambda function an used the Code from here changed the file name form "CreateThumbnail.js" to "index.js".

在此输入图像描述

Set the trigger, on create - so the thumbnails should be created when a user of my App uploads an image to the bucket:

在此输入图像描述

When I test the function, it works for the first time (with the Test button on the lambda website on amazon) but it doesn't work when a User uploads an image from my app nor it works when I upload an image on the s3 bucket on amazons website.

When I try to test the function for the second time with the "Test" button, I get this error (yes I deleted the old resized-HappyFace.jpg from the thumbnails bucket):

在此输入图像描述

What have I forgotten?

From the logs, it seems that your lambda function taking too much time to process the request. Try increasing the timeout and then test. You can set the timeout from Configuration tab -> Advanced settings -> Timeout.

在此输入图像描述

The solution for this problem was the policy. The timout error for the testing above could be solved by increasing the timeout limit to 25sec.

How to set the policy:

Go to IAM => Roles => Create a new Role => Role Type => AWS Lambda => in Attach Policy select "AmazonS3FullAccess"

Now go to Lambda => select your Lambda function => in Configuration tab => Role: choose an existing Role => Select the just created Role

And finish, now it works. :)

Create thumbnails for already existing images:

You may have existing images in your bucket, which you want to create thumbnails for them? Solution: Create a new bucket - go to the old bucket and select all images => cut. go to the new temporary created bucket and paste. Now select all those newly pasted images again in the temporary bucket => cut - go back to the old bucket and paste the images => this triggers your lambda function and will create for every image a new thumbnail. now you can delete the temporary created bucket. you are done. :)

If you run into difficulties, go to the Monitoring tab (shown in your picture) and click the link to the log files . They will hopefully give you some more information.

Actually, I suggest that you start by implementing the Tutorial: Using AWS Lambda with Amazon S3 from the AWS documentation.

Once you have confirmed that it is working, modify it for your particular needs (eg buckets, permissions).

Alternatively, you could just skip Lambda and use an automatic picture resizing service such as:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM