I am creating a function on AWS Lambda to take screenshots of websites and save the resulting image to S3. The function is successfully creating a screenshot and saving to S3, however it is ignoring the parameters given to the page.screenshot() function and just using defaults.
const chromium = require('chrome-aws-lambda');
const aws = require('aws-sdk');
var s3 = new aws.S3();
exports.handler = async (event, context, callback) => {
let result = null;
let resultKey = null;
let browser = null;
try {
resultKey = event.url.replace(/[W_]+/g,"_") + '.png';
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
let page = await browser.newPage();
await page.goto(event.url || 'https://example.com');
result = await page.screenshot({
type: 'jpeg',
fullpage: true
});
// Image ready. Send to S3
var params = {
Bucket: 'mybucket',
Key: resultKey,
Body: result
};
s3.putObject(params, function(err, data) {
if (err) {
console.error(err, err.stack);
} else {
console.log(data);
}
});
} catch (error) {
return callback(error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return callback(null, result);
};
So the image created ignores the type and fullpage parameters, and creates a .png image that is not fullpage.
What am I doing wrong?