-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (35 loc) · 1.06 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
const AWS = require('aws-sdk');
const gm = require('gm').subClass({ imageMagick: true });
const s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
const Bucket = event.Records[0].s3.bucket.name;
const Key = event.Records[0].s3.object.key;
const filename = Key.split('/')[Key.split('/').length - 1];
const ext = Key.split('.')[Key.split('.').length - 1];
s3.getObject({ Bucket, Key }, (err, data) => {
if (err) {
console.log(`Image load error: ${err}`);
return callback(err);
}
return gm(data.Body)
.resize(200, 200, '^')
.quality(90)
.toBuffer(ext, (err, buffer) => {
if (err) {
console.log(`Image resizing err: ${err}`);
return callback(err);
}
return s3.putObject({
Bucket,
Key: `thumb/${filename}`,
Body: buffer,
}, (err) => {
if (err) {
console.log(`Image upload err: ${err}`);
return callback(err);
}
return callback(null, `goods/${filename}`);
})
});
});
};