首页 > 解决方案 > 通过流将数据块写入 S3 文件,而不是创建临时文件 Node.js

问题描述

我正在尝试将 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 信息块。

标签: javascriptnode.jsamazon-web-servicesamazon-s3

解决方案


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


推荐阅读