-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (45 loc) · 1.24 KB
/
index.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 s3 = new aws.S3();
const sharp = require('sharp');
exports.handler = (event, _, callback) => {
console.log(event.queryStringParameters);
console.log(event.pathParameters);
const bucket = process.env.BUCKET;
const key = event.pathParameters.proxy;
const options = event.queryStringParameters || {};
const params = {
Bucket: bucket,
Key: key,
};
s3.getObject(params, (err, rawImageData) => {
if (err) {
callback(null, {
statusCode: 404,
headers: {},
body: '',
});
} else {
const image = sharp(rawImageData.Body);
if (options.w) {
image.resize(parseInt(options.w, 10), null);
}
if (options.blur) {
image.blur(parseFloat(options.blur), null);
}
image.toBuffer((imageErr, data, info) => {
console.log(imageErr);
console.log(info);
callback(null, {
statusCode: 200,
body: data.toString('base64'),
isBase64Encoded: true,
headers: {
'Content-Type': `image/${info.format}`,
'Cache-Control': 'public, max-age=315360000',
'X-Aspect-Ratio': (info.width / info.height),
},
});
});
}
});
};