【发布时间】: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