使用Excel.js将文件写入s3存储桶

时间:2019-08-17 19:44:54

标签: amazon-s3 exceljs

下面的脚本从mySql查询并将结果存储在本地excel中。我要解决的问题是将其保存到S3存储桶中,而不是保存在我的计算机上。我对AWS有一定的经验,可以桥接到AWS以从S3存储桶读取文件,但是我很难解决这个问题。谢谢!

我找到了以下代码,但无法将其与已经编写的代码集成。

const s3 = new aws.S3(/* put your s3 configuration here */);
const stream = new Stream.PassThrough();
const workbook = new exceljs.Workbook();
// Add images of cats to workbook

workbook.xlsx.write(stream)
    .then(() => {
        return s3.upload({
            Key: PATH_IN_S3,
            Bucket: BUCKET_NAME,
            Body: stream,
            ContentType: CONTENT_TYPE_EXCEL
        }).promise();
    })
    .then(/* do whatever */)
    .catch(/* handle error */);

Here is what is currently working locally:


const reconQuery = 'SELECT T1.Database_ID, Lease_Description, SUM(BR_Current_Month_Cash - BR_Current_Month_Cash_Client) total, SUM(CAM_Current_Month_Cash - CAM_Current_Month_Cash_Client) total2, SUM(TAX_Current_Month_Cash - TAX_Current_Month_Cash_Client) total3, SUM(Insurance_Current_Month_Cash - Insurance_Current_Month_Cash_Client) total4, SUM(Sales_Tax_Current_Month_Cash - Sales_Tax_Current_Month_Cash_Client) total5  FROM `lq 2` AS T1  INNER JOIN `cd 2` AS T2 ON T1.Database_ID = T2.Database_ID GROUP BY Database_ID'


  connection.query(reconQuery,
    function (err, reconQuery, field) {

      const jsonReconData = JSON.parse(JSON.stringify(reconQuery));


      let workbook = new excel.Workbook(); //creating workbook
      let worksheet = workbook.addWorksheet('Month 2'); //creating worksheet

      //  WorkSheet Header
      //  WorkSheet Header
      worksheet.columns = [
        { header: 'Database ID', key: 'Database_ID', width: 10 },
        { header: 'Lease Description', key: 'Lease_Description', width: 30 },
        { header: 'Base Rent Difference', key: 'total', width: 30 },
        { header: 'CAM Difference', key: 'total2', width: 30 },
        { header: 'Tax Difference', key: 'total3', width: 30 },
        { header: 'Insurance Difference', key: 'total4', width: 30 },
        { header: 'Sales Tax Difference', key: 'total5', width: 30 },
      ];

      // Add Array Rows
      worksheet.addRows(jsonReconData);



      // Write to File
      workbook.xlsx.writeFile("month2.xlsx")
        .then(function () {
          console.log("file saved!");
        });
    })

1 个答案:

答案 0 :(得分:0)

我想出是否有人感兴趣:

const stream = new Stream.PassThrough();
const workbook = new excel.Workbook();
let worksheet = workbook.addWorksheet('Month 1'); //creating worksheet
     //  WorkSheet Header
  worksheet.columns = [
    { header: 'Database ID', key: 'Database_ID', width: 10 },
    { header: 'Lease Description', key: 'Lease_Description', width: 30 },
    { header: 'Base Rent Difference', key: 'total', width: 30},
      { header: 'CAM Difference', key: 'total2', width: 30},
      { header: 'Tax Difference', key: 'total3', width: 30},
      { header: 'Insurance Difference', key: 'total4', width: 30},
      { header: 'Sales Tax Difference', key: 'total5', width: 30},
  ];
// Add Array Rows
worksheet.addRows(jsonReconData);

workbook.xlsx.write(stream)
    .then(() => {
        return s3.upload({
            Key: 'Month1.xlsx',
            Bucket: 'bucketname',
            Body: stream,
            ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        }).promise();
    })
    .catch(function(e) {
      console.log(e.message)
    }).then(function(){
      console.log('after a catch the chain is restored');
    }, function () {
      console.log('Not fired due to the catch');
    });