forked from aws-samples/lambda-refarch-fileprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-processor-2.js
More file actions
75 lines (65 loc) · 2.1 KB
/
data-processor-2.js
File metadata and controls
75 lines (65 loc) · 2.1 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
/* Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use
this file except in compliance with the License. A copy of the License is
located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions and
limitations under the License. */
var AWS = require('aws-sdk');
var removeMD = require('remove-markdown');
var async = require('async');
var s3 = new AWS.S3();
function getSNSMessageObject(msgString) {
var x = msgString.replace(/\\/g, '');
var y = x.substring(1, x.length - 1);
var z = JSON.parse(y);
return z;
}
exports.handler = function (event, context) {
console.log('event: ' + JSON.stringify(event));
var snsMsgString = JSON.stringify(event.Records[0].Sns.Message);
var snsMsgObject = getSNSMessageObject(snsMsgString);
var obj = {
'bucket': snsMsgObject.Records[0].s3.bucket.name,
'bucketOut': String(snsMsgObject.Records[0].s3.bucket.name + "-out"),
'key': snsMsgObject.Records[0].s3.object.key
};
async.waterfall([
function download(next) {
// get Markdown object
s3.getObject(
{
Bucket: obj.bucket,
Key: obj.key
},
next);
},
function transform(response, next) {
// strip out md and convert to plaintext
var data = removeMD(String(response.Body));
next(null, data);
},
function upload(data, next) {
// change file extension
var newFileName = obj.key.split(".")[0] + ".txt";
console.log("Uploading data to: " + obj.bucketOut);
s3.putObject(
{
Bucket: obj.bucketOut,
Key: newFileName,
Body: data,
ContentType: "text/plain" // set contentType as plaintext
},
next);
}
], function (err) {
if (err) {
console.error(err);
} else {
console.log('Success');
}
context.done();
});
};