-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
29 lines (26 loc) · 1.2 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
'use strict';
const bucketName = process.env.CUSTOMER_IMAGE_BUCKET_NAME;
const region = process.env.REGION;
const s3Client = require('./facade/s3')(bucketName, region);
const sharp = require('sharp');
const { PassThrough } = require('stream');
const postProcessor = async (event) => {
try {
console.log(`about to fetch s3 object ${event.Records[0].s3.object.key} from bucket ${bucketName} and region ${region} ${JSON.stringify(event)}`);
const object = await s3Client.getObject(event.Records[0].s3.object.key);
console.log(`fetched s3 object with content length ${object.ContentLength}`);
const resizedObjectKey = event.Records[0].s3.object.key.replace('uploaded', 'thumbnails');
const writeResponse = await s3Client.writeObject(resizedObjectKey, getResizedStream(object));
console.log(`finished creating a thumbnail object ${writeResponse}`);
return true;
} catch(e) {
console.log(`error occurred while trying to resize object ${e.stack}`);
}
}
const getResizedStream = (object) => {
const resizer = sharp().resize(50).png();
const writeStream = new PassThrough();
object.Body.pipe(resizer).pipe(writeStream);
return resizer;
}
module.exports.postProcessor = postProcessor;