NodeJS 使用节点从s3存储桶读取Csv

xoshrz7s  于 2023-03-08  发布在  Node.js
关注(0)|答案(1)|浏览(153)

需要一些帮助,我有一个AWS lambda,我从csv中获取数据并解析它,以便计算csv中的总行数。
我尝试过不同的方法来计算行数并将这些信息存储在输出存储桶中。下面是我最后一次尝试这样做。我有一个输出,但没有计算行数。我执行了console.log,看到rowCount变量增加,但在输出文档中没有增加。

import { S3 } from '@aws-sdk/client-s3';
import { parse } from 'csv';

const s3 = new S3();
let rowCount = 0;

export const handler = async (event) => {
  const bucket = event.Records[0].s3.bucket.name;
  const key = event.Records[0].s3.object.key;

  const params = {
    Bucket: bucket,
    Key: key,
  };

  // Download the file from the input bucket
  const doc = await s3.getObject(params);

  doc.Body.pipe(
    parse({
      delimiter: ",",
      columns: true,
    })
    .on('data', function (row) {
        console.log(row);
        rowCount++;
        console.log(rowCount)
      })

    .on('end', async function(){
        console.log(rowCount)
    })
  )
  
  const date = new Date().toLocaleString();
  const outputBucket = 'outputbucket';
  const outputKey = 'output.txt';
  const outputParams = {
    Bucket: outputBucket,
    Key: outputKey,
    Body: `Processed ${rowCount} rows in file ${key} from ${bucket} on ${date}`,
  };
      
  await s3.putObject(outputParams)

};
hjqgdpho

hjqgdpho1#

为解决rowCount变量未正确更新和未生成输出文本文件的问题而进行的更改基于等待CSV文件处理完成后再创建输出文本文件。这是通过创建一个承诺来实现的,该承诺在CSV文件处理完成后立即解决。并在创建输出文本文件之前使用wait来等待promise解析,这样可以确保在创建输出文本文件之前正确地更新rowCount变量。

import { S3 } from '@aws-sdk/client-s3';
import { parse } from 'csv';

const s3 = new S3();

export const handler = async (event) => {
  const bucket = event.Records[0].s3.bucket.name;
  const key = event.Records[0].s3.object.key;

  const params = {
    Bucket: bucket,
    Key: key,
  };

  let rowCount = 0;

  const doc = await s3.getObject(params);

  const processCsvPromise = new Promise((resolve, reject) => {
    doc.Body.pipe(
      parse({
        delimiter: ',',
        columns: true,
      })
    )
      .on('data', function (row) {
        console.log(row);
        rowCount++;
      })
      .on('end', function () {
        console.log(`Rows: ${rowCount}`);
        resolve();
      });
  });

  await processCsvPromise;

  const date = new Date().toLocaleString();
  const outputBucket = 'outputbucket';
  const outputKey = 'output.txt';
  const outputParams = {
    Bucket: outputBucket,
    Key: outputKey,
    Body: `${rowCount} rows in the file ${key} from ${bucket} at ${date}`,
  };
  await s3.putObject(outputParams);
};

相关问题