【问题标题】:Write data chunks to S3 file through stream instead of creating temporary file Node.js通过流将数据块写入 S3 文件,而不是创建临时文件 Node.js
【发布时间】:2020-01-27 04:20:58
【问题描述】:

我正在尝试将 CSV 格式的数据块写入 Amazon S3 中的文件,而不是通过 WriteStream 写入临时文件,然后在该文件上创建 ReadStream 并将其发送到 S3。我的程序从数据库中提取数据行,对其进行处理,然后使用 S3 的 upload() api 将每一行格式化为 CSV 格式

let recordsCSVFormatted;
let offset = 0;
const batchSize = 500;
const writer = fs.createWriteStream('./someFile.csv')

do {
  recordsCSVFormatted = await getRecords(limit, offset); // gets records from DB, formats it in CSV string
  writer.write(recordsCSVFormatted);
  offset += batchSize;
} while (typeof recordsCSVFormatted === 'undefined' || (recordsCSVFormatted && recordsCSVFormatted.length))

const reader = fs.createReadStream('./someFile.csv');

// just assume here that Key and Bucket are provided in upload, they are in actual code
await new AWS.S3({...s3Opts}).upload({Body: reader}).promise() // pass the readable in here for AWS

如何跳过创建临时文件然后将文件作为流传递给 AWS 的步骤?我希望能够直接流式传输 CSV 信息块。

【问题讨论】:

  • 创建一个可读流并将您的 CSV 记录推送到那里。使用 S3 客户端 upload() 选项,其中 Body 的类型为 ReadableStream。
  • 每次迭代都是一个只有 1 个 Csv 文件的块。完成循环的所有迭代后,写入流关闭,生成 1 个 CSV 文件。我尝试通过创建一个可读的并将块推送到它来做你已经说过的事情,但也许我做得不对。你能举个例子吗?

标签: javascript node.js amazon-web-services amazon-s3


【解决方案1】:

通过实现 Readable 类并实现自定义 read() 函数以供 S3 上传使用来解决此问题

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多