[英]Reading Multiple files and writing to one file Node.JS
我目前正在尝试使用 Node.js 制作数据管道当然,这不是制作它的最佳方法,但我想在对其进行改进之前尝试实现它。
这是情况
我在 AWS S3 上有多个 gzip 压缩的 csv 文件。 我使用 aws sdk 获得这些“对象”
像下面这样,把它们变成 readStream
const unzip = createGunzip()
const input = s3.getObject(parameterWithBucketandKey)
.createReadStream()
.pipe(unzip)
并使用上面的流创建 readline 接口
const targetFile = createWriteSTream('path to target file');
const rl = createInterface({
input: input
})
let first = true;
rl.on('line', (line) => {
if(first) {
first = false;
return;
}
targetFile.write(line);
await getstats_and_fetch_filesize();
if(filesize > allowed_size){
changed_file_name = change_the_name_of_file()
compress(change_file_name)
}
});
这被包装成一个承诺
我有要从 AWS S3 检索的文件名数组,并像这样映射这些文件名数组
const arrayOfFileNames = [name1, name2, name3 ... and 5000 more]
const arrayOfPromiseFileProcesses= arrayOfFileNames.map((filename) => return promiseFileProcess(filename))
await Promise.all(arrayOfPromiseFileProcesses);
// the result should be multiple gzip files that are compressed again.
对不起,我用伪代码写的,如果它需要更多来提供上下文,那么我会写更多,但我认为这会给出我的问题的一般内容。
我的问题是它可以很好地写入文件,但是当我更改 file_name 时,它之后不会创建一个。 我迷失在这个同步和异步的世界中......
请给我一个提示/参考以供阅读。 谢谢你。
行事件处理程序必须是异步函数,因为它调用 await
rl.on('line', async(line) => {
if(first) {
first = false;
return;
}
targetFile.write(line);
await getstats_and_fetch_filesize();
if(filesize > allowed_size){
changed_file_name = change_the_name_of_file()
compress(change_file_name)
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.