问题
I'm trying to read an excel file present in S3 using node.js integrated with lambda. I mean the code should interact with excel file and display the output. Please help me in this issue.
回答1:
To read a file from S3 in AWS lambda with nodejs you can follow given steps. To read data from excel file in node js, I prefer xlsx
package. To use it, first you need to install xlsx
node package and then proceed as given -
npm i xlsx --save
Then you can read excel file as -
const xlsx = require('xlsx');
var params = {
Bucket: "",
Key: ""
};
var file = s3.getObject(params).createReadStream();
var buffers = [];
file.on('data', function (data) {
buffers.push(data);
});
file.on('end', function () {
var buffer = Buffer.concat(buffers);
var workbook = xlsx.parse(buffer);
console.log("workbook", workbook);
var sheet_name_list = workbook.SheetNames;
//if you have multiple sheets
data = xlsx.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);
for(var key in data){
console.log(data[key]['yourColumn']);
}
});
来源:https://stackoverflow.com/questions/50848483/how-to-read-an-excel-file-using-node-js