如何在节点js中使用aws-sdk在s3中存储Excel文件?

时间:2017-03-12 10:05:00

标签: node.js excel buffer aws-sdk aws-sdk-nodejs

    var xls = json2xls(data);
    var bufferdata = new Buffer(xls).toString('base64');
    console.log("inside upload s3 function........", data);
    //var buf = new Buffer(data, 'base64');
    var uploaddata = {
        Key: "exception.xls",
        Body: bufferdata,
        ContentEncoding: 'base64',
        ContentType: 'application/vnd.ms-excel'
    };

我可以通过s3保存和检索文件。但格式不匹配即将到来。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

尝试一下,为我工作。

const xls = json2xls(data);

const buffer = Buffer.from(xls, 'binary');

console.log("inside upload s3 function........", data);
var uploaddata = {
    Key: "exception.xls",
    Body: buffer,
    ContentType: 'application/vnd.ms-excel'
};

答案 1 :(得分:0)

Sameer解决方案非常适合xlsx / xls文件类型。

对于CSV。我使用了json2csv包裹。

const { Parser } = require('json2csv');
const fields = ['id', 'title', 'description'];
const opts = { fields };
const parser = new Parser(opts);
const csv = parser.parse(your data);

const uploaddata = {
Key: "exception.csv",
Body: csv,
ContentType: 'application/vnd.ms-excel'
};

快乐编码;)