forked from simalexan/s3-lambda-csv-to-xlsx-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
23 lines (20 loc) · 930 Bytes
/
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
const s3Util = require('./s3-util'),
path = require('path'),
convert = require('./convert'),
os = require('os'),
EXTENSION = process.env.EXTENSION,
OUTPUT_BUCKET = process.env.RESULTS_BUCKET_NAME,
MIME_TYPE = process.env.MIME_TYPE;
exports.handler = function (eventObject, context) {
const eventRecord = eventObject.Records && eventObject.Records[0],
inputBucket = eventRecord.s3.bucket.name,
key = eventRecord.s3.object.key,
id = context.awsRequestId,
resultKey = key.replace(/\.[^.]+$/, EXTENSION),
tempPath = path.join(os.tmpdir(), id),
convertedPath = path.join(os.tmpdir(), 'converted-' + id + EXTENSION);
console.log('converting', inputBucket, key, 'using', tempPath);
return s3Util.downloadFileFromS3(inputBucket, key, tempPath)
.then(() => convert(tempPath, convertedPath))
.then(() => s3Util.uploadFileToS3(OUTPUT_BUCKET, resultKey, convertedPath, MIME_TYPE));
};