-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (57 loc) · 2.39 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
53
54
55
56
57
58
59
60
61
62
63
const aws = require('aws-sdk');
aws.config.update({
accessKeyId: process.env.accessKeyId,
secretAccessKey: process.env.secretAccessKey,
region: process.env.region
});
const s3 = new aws.S3();
const zlib = require('zlib');
const gzip = zlib.createGzip();
exports.handler = (event, context, callback) => {
var keyName = event.Records[0].s3.object.key;
s3.getObject({ Bucket: process.env.sourceBucket, Key: keyName }, function (err, data) {
if (!err) {
let fileContent = data.Body.toString();
zlib.gzip(fileContent, (err, buffer) => {
if (!err) {
let contentType = '';
let extension = keyName.split('.').pop();
extension = extension.toString().trim().toLowerCase();
switch(extension){
case 'js':
case 'map':
contentType = 'application/json';
break;
case 'json':
contentType = 'text/html';
break;
case 'html':
contentType = 'text/html';
break;
case 'svg':
contentType = 'image/svg+xml';
break;
case 'png':
contentType = 'image/png';
break;
case 'jpg':
case 'jpeg':
contentType = 'image/jpeg';
break;
case 'gif':
contentType = 'image/gif';
break;
default:
contentType = 'binary/octet-stream';
break;
}
const params = { Bucket: process.env.bucket, Key: keyName, Body: buffer, ContentType: contentType, ContentEncoding: 'gzip' };
s3.putObject(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
});
}
});
};