Download xlsx from S3 and parse it

前端 未结 5 1720
星月不相逢
星月不相逢 2021-01-05 10:33

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

The problem is that I can\'t get xlsx to parse the file. When I try to read back

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 11:00

    The node-xlsx module requires that the entire xlsx buffer be available. So you cannot pass it a ReadStream like you're currently doing. Try this method which entirely avoids writing to disk:

    router.get('/process', (req, res) => {
        var fileName = 'https://some-bucket.s3.amazonaws.com/some-excel-file.xlsx'
        https.get(fileName, response => {
            var chunks = []
            response.on('data', chunk => chunks.push(chunk))
            .on('end', () => {
                var book = xlsx.parse(Buffer.concat(chunks))
                book.forEach(sheet => console.log('sheet', sheet.name))
                res.status(200)          
            })
            .on('error', e => {
                res.status(500)
            })
        })
        return
    })
    

提交回复
热议问题