lmc
lmc

Reputation: 279

Uploading screenshot from AWS lambda to s3 bucket fails

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");

}

Upvotes: 1

Views: 1367

Answers (1)

lmc
lmc

Reputation: 279

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.

Upvotes: 2

Related Questions