Download xlsx from S3 and parse it

前端 未结 5 1680
星月不相逢
星月不相逢 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:11

    Another way of doing this is using exceljs

    const AWS = require('aws-sdk');
    const Excel = require('exceljs');
    
    async function downloadFile(){
        AWS.config.update({
          accessKeyId: AMAZON_ACCESS_KEY,
          secretAccessKey: AMAZON_SECRET_ACCESS_KEY,
        });
    
        const s3 = new AWS.S3();
        const stream = await s3.getObject({ Bucket: 'yor_buket', Key: 'file_name'}).createReadStream();
        return stream;
    }
    
    
    async function loadWorkbook(stream){
    
        return new Promise((resolve, reject) = > {
            let rows = [];
            const workbook = new Excel.Workbook();
    
            workbook.xlsx.read(stream).then(function(workbook){
    
                const worksheet = workbook.getWorksheet('sheet_name');
    
                worksheet.eachRow({ includeEmpty: false}, function(row) {
                    rows.push(row.values); 
                });
            });
            resolve(rows);
        });
    
    }
    
    async function loadFromS3(){
        const stream = await downloadFile();
        const dataRows = await loadWorkbook(stream);
        console.log(dataRows);
    }
    

提交回复
热议问题