javascript - 通过流将数据块写入 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 信息块。
解决方案
通过实现 Readable 类并实现自定义 read() 函数以供 S3 上传使用来解决此问题
推荐阅读
- r - R图线性拟合子集
- python-3.x - 迭代添加的最有效数据类型?
- javascript - 如何比较两个数字一是相同输入的输入值和属性值?
- mysql - MySql 查询速度慢 1000 倍,LIMIT 稍高(不是偏移量)
- android - BLE:如何在扫描结果之间准确找到我的设备?
- python - python中类实例化期间实例与对象之间的区别
- javascript - 通过 Symbol.iterator 的迭代不会发生
- powershell - 如何使用 powershell 5 创建具有 2 个密钥用法的证书
- database - 如何在不覆盖实时数据的情况下更新来自测试数据库的实时数据库中的列内的表
- php - Angular 5 Ionic 3 Laravel 项目结构