从S3下载xlsx并解析 [英] Download xlsx from S3 and parse it

查看:189
本文介绍了从S3下载xlsx并解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一项服务来从Amazon S3下载excel文件,然后使用 node-xlsx

I need a service to download an excel file from Amazon S3, then parse with node-xlsx

问题是我无法让xlsx解析文件.当我尝试回读刚写的文件时,代码中找不到该文件.

The problem is that I can't get xlsx to parse the file. When I try to read back the file I just wrote, it isn't found by the code.

我不太确定这是否是最好的方法,但这是我到目前为止得到的:

I'm not quite sure if this is the best approach, but this is what I got so far:

router.get('/process', (req, res) => {
    var fileName = 'https://some-bucket.s3.amazonaws.com/some-excel-file.xlsx'
    https.get(fileName, response => {
        var body = ''
        response.on('data', chunk => body += chunk)
        response.on('end', () => {

            //fs is being imported early on this file
            fs.writeFile(__dirname + '/test.xlsx', body)

            var f = fs.createReadStream(__dirname + '/test.xlsx')

            var book = xlsx.parse(f)
            book.forEach(sheet => console.log('sheet', sheet.name) )

            res.status(200)          
        })
        .on('error', e => {
            res.status(500)
        })
    })
    return
})

推荐答案

这是您可以从S3 nodejs读取文件并将其保存在内存中的方式,而无需先将文件写入磁盘上的某个位置.它可以与S3和AWS Lambda结合使用,因此您不必将文件写入Lambda上的某个位置.

This is how you can read a file from S3 nodejs and keep it in memory without first writing the file to some location on disk. It can be used with a combination of S3 and AWS Lambda so that you don't have to write the files to some location on the Lambda.

请记住,此过程是异步的.

Remember this processes is asynchronous.

   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)
    });

这篇关于从S3下载xlsx并解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆