-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
64 lines (57 loc) · 1.92 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const { v4: uuidv4 } = require('uuid');
const chromium = require('chrome-aws-lambda');
const AWS = require('aws-sdk');
const S3 = new AWS.S3({
signatureVersion: 'v4',
});
const bucketName = process.env.BUCKET_NAME;
module.exports.screenshotToS3 = async (event, context, callback) => {
let result = null;
let browser = null;
try {
// launch the browser
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
// navigate to the url and take a screenshot
let page = await browser.newPage();
await page.goto(event.url || 'https://www.serverless.com');
const buffer = await page.title().then(() => {
return page.screenshot({type: 'png'});
}
);
// upload the screenshot in s3
const bucketParams = {
Body: buffer,
Bucket: bucketName,
ContentType: 'image/png',
CacheControl: 'max-age=31536000',
Key: uuidv4() + '.png',
StorageClass: 'STANDARD'
};
result = await S3.putObject(bucketParams).promise().then((data) => {
console.log('Successfully PUT object');
console.log(data);
return {
screenshotUrl: `https://${bucketName}.s3.amazonaws.com/${bucketParams.Key}`,
...data
};
}).catch((error) => {
console.log('Failed to PUT object');
console.log(error);
return error;
});
} catch (error) {
return callback(null, error);
} finally {
if (browser !== null) {
await browser.close();
}
}
return callback(null, result);
};