-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
74 lines (59 loc) · 2.2 KB
/
handler.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
64
65
66
67
68
69
70
71
72
73
74
const aws = require("aws-sdk");
const csv2json = require("csvtojson");
const json2csv = require("json-2-csv");
module.exports.triggerS3Operations = async (event, _context, _callback) => {
console.log(`Event object: ${event}`);
try {
console.log(`Lambda process STARTED!`);
aws.config.setPromisesDependency();
const s3 = new aws.S3();
console.log(`Initiated s3 from AWS-SDK!`);
const data = s3
.getObject({
Bucket: "src-kitten",
Key: "realestate.csv",
})
.createReadStream();
console.log(`realestate.csv read stream operation: DONE!`);
const res = await csv2json().fromStream(data);
console.log(`Steamed data - json convertio: DONE!`);
const conJsonData = req.map((data) => {
return {
street: data.street,
city: data.city,
state: data.state,
zip: data.zip,
type: data.type,
price: data.price,
};
});
console.log(`JSON data is consolidate: DONE!`);
console.log(`JSON - CSV string convertion beginning...`);
const convertedCsvData = await json2csv.json2csvAsync(
conJsonData,
(err, csv) => {
if (err) return err;
return csv;
}
);
console.log(`JSON - CSV string convertion OVER!`);
console.log(`ConvertedCsvData => ${convertedCsvData}`);
const params = {
Bucket: "dest-kitten",
Key: "consolidate-realestate.csv",
Body: Buffer.from(convertedCsvData),
};
console.log(`Param creation for s3 target bucket: DONE!`);
console.log(`s3 bucket writing process going to start!!!`);
await s3
.putObject(params)
.promise()
.then((data) => {
console.log(`File uploaded successfull - data ${data}`);
console.log(`Lambda operation over: SIGNING OFF!!`);
})
.catch((err) => console.log(`Error occured at putObject: ${err}`));
} catch (e) {
console.log(`Error occured: ${e}`);
}
};