Skip to content

Instantly share code, notes, and snippets.

@NguyenDa18
Created February 27, 2020 19:49
Show Gist options
  • Save NguyenDa18/ecdcc78d3b8a1f4adc166d33875a6ece to your computer and use it in GitHub Desktop.
Save NguyenDa18/ecdcc78d3b8a1f4adc166d33875a6ece to your computer and use it in GitHub Desktop.
AWS Excel Read + Update DB Lambda
const AWS = require('aws-sdk')
AWS.config.update({
region: "<region>",
});
const s3 = new AWS.S3()
const docClient = new AWS.DynamoDB.DocumentClient();
const xlsx = require('node-xlsx')
const testfilename = "<file>.xlsm"
// src: https://stackoverflow.com/questions/40535757/download-xlsx-from-s3-and-parse-it
exports.handler = (event, context, callback) => {
const params = {
Bucket: "<bucket>",
Key: testfilename
};
let buffers = [];
const stream = s3.getObject(params).createReadStream();
stream.on('data', function (data) {
buffers.push(data);
});
stream.on('end', function () {
const buffer = Buffer.concat(buffers);
const workbook = xlsx.parse(buffer);
const [header, ...dataRows] = workbook[0].data
dataRows.forEach((row, i) => {
populateDB(row)
})
context.succeed('done')
});
stream.on('error', (err) => {
console.error(`--- ERROR ---`)
console.error(err)
});
}
const populateDB = row => {
const TableName = '<table name>'
const Item = {
a: row[0],
b: row[4],
c: row[5],
d: row[8],
e: row[7],
f: row[15]
}
const dbparams = {
TableName,
Item
}
docClient.put(dbparams, function (err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
}
});
}
@Omid-M22
Copy link

great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment