Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting Puppeteer screenshot to S3

I'm trying to do something similar to what this person did but I'm having difficulty figuring out how to redirect the output of Puppeteer screenshotting directly to S3 as the screenshot function only has a parameter for local directory paths. Storing the images on Lambda is an option but not a preferred one.

like image 887
Andrey Choi Avatar asked Dec 05 '22 11:12

Andrey Choi


1 Answers

Passing a path to screenshot is optional. You can omit the path and use the Promise<[Buffer|String]>> it returns as the content for a new S3 object (after awaiting it). Assuming you have the AWS SDK and a bucket set up already, your configuration will look something like this:

const AWS = require("aws-sdk");
const s3 = new AWS.S3();
const bucket = "your.bucket.name";
const key = "yourObjectKey";

Then, wherever you’re ready to take the screenshot:

const screenshot = await page.screenshot();
const params = { Bucket: bucket, Key: key, Body: screenshot };
await s3.putObject(params).promise();

Depending on what you intend to use it for, you may want to add ContentType or other relevant headers to the parameters. You can see the list in the documentation.

like image 129
Aankhen Avatar answered Dec 09 '22 14:12

Aankhen