-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadScreenshot.js
47 lines (40 loc) · 1.4 KB
/
uploadScreenshot.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
const aws = require("aws-sdk");
const fs = require("fs");
// Your S3 bucket from earlier. Make sure you use the same name
// used in serverless.yml
const BUCKET = process.env.BUCKET_NAME;
// hardcoded
//const BUCKET = "pwnppeteer";
// uploadScreenshot takes a temporary file path and returns a URL
exports.uploadScreenshot = async function uploadScreenshot(
path,
screenshot,
resize = false
) {
// aws-sdk is all callback-based so we have to wrap in Promises, yuck
return new Promise((resolve, reject) => {
const s3 = new aws.S3({
apiVersion: "2006-03-01"
});
(async function() {
const buffer = await new Promise((resolve, reject) => {
// reading the file
fs.readFile(path, (err, data) => {
if (err) reject(err);
resolve(data);
});
});
// uploading the file buffer
const { Location } = await s3
.upload({
Bucket: BUCKET,
Key: `${screenshot}-${new Date().getTime()}.png`,
Body: buffer,
// Set the approriate ACL that fit your need
ACL: "public-read"
})
.promise();
resolve(Location);
})();
});
};