Uploading screenshot from AWS lambda to s3 bucket fails

家住魔仙堡 提交于 2019-12-13 06:19:40

问题


I am trying to take screenshots with puppeteer on aws lambda and upload the screenshot to a s3 bucket. However, the s3.putObject method doesn't seem to be working. On the lambda console, I got both the "uploading screenshot 's3://${s3bucket}/${filename}'" and "uploading completed" message but not the "inside callback" message. The weird thing is, I got no error during the lambda execution, but I just couldn't get the message inside the putObject method and couldn't find the screenshots in the bucket. Can anyone give me some suggestion on how to debug?

const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });

module.exports.saveScreenshotToS3 = async(page, s3bucket, filename) => {
    let buffer = await page.screenshot({encoding: "base64"});
    console.log(`Uploading screenshot 's3://${s3bucket}/${filename}'`);
    const s3Params = {
        Bucket: s3bucket,
        Key: filename,
        Body: buffer
    };
    await s3.putObject(s3Params, (err, data) => {
        console.log("inside callback");
        if (err) {
            console.log(err);
        } else {
            console.log("uploading succeeded");
        }
    }).promise();
    console.log("uploading completed");

}

回答1:


Resolved.

I only changed permission in bucket policy and forgot to change the CORS configuration, and that's what made my function fail.

I added <AllowedMethod> PUT </AllowedMethod> to my CORS configuration script and the uploading works.

Remember to change both the bucket policy and CORS configuration so that you have the right permission to upload files to your bucket.



来源:https://stackoverflow.com/questions/50806696/uploading-screenshot-from-aws-lambda-to-s3-bucket-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!