-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
52 lines (43 loc) · 1.45 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
48
49
50
51
52
import url from 'url';
import imageResizer from './src/imageResizer';
const isS3Event = event => typeof event.Records === 'object';
const isApiGatewayEvent = event => typeof event.Records === 'undefined';
const sourceImage = (event) => {
let bucket = '';
let key = '';
let options = {};
if (isS3Event(event)) {
const s3 = event.Records[0].s3;
bucket = s3.bucket.name;
key = s3.object.key;
}
if (isApiGatewayEvent(event)) {
const u = url.parse(event.queryStringParameters.source_url);
bucket = u.hostname;
key = u.path.replace(/^\//, '');
options = {
resizeOption: event.queryStringParameters.resize_option,
destS3Bucket: event.queryStringParameters.dest_s3_bucket,
destS3Prefix: event.queryStringParameters.dest_s3_prefix,
};
}
return { bucket, key, options };
};
const successResponse = data =>
// NOTE: lambda proxy integration format
({
isBase64Encoded: false,
statusCode: 201,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
export const handle = (event, context, callback) => {
console.log('eventObject: %s', JSON.stringify(event, null, 2));
console.log('context: %s', JSON.stringify(context, null, 2));
const image = sourceImage(event);
imageResizer({ sourceBucket: image.bucket, sourceKey: image.key, options: image.options })
.then(data => callback(null, successResponse(data)))
.catch(err => callback(err));
};