【发布时间】:2020-06-26 02:29:24
【问题描述】:
我在 s3 中有一个包含数百个 csv 文件的 zip。我正在尝试流式传输文件并需要读取文件的前 n 行。我能够解压缩它并读取内容,但不知道当我读完 n 行并继续处理其余文件时如何停止流。
到目前为止尝试过的代码
const aws = require("aws-sdk");
const s3 = new aws.S3();
const etl = require("etl");
const unzip = require("unzip-stream");
function setupMetadata() {
s3.getObject({Bucket: 'test', Key: 'CSV.zip'}).createReadStream()
.pipe(unzip.Parse())
.on('entry', function (entry) {
var i = 0;
var recordIdentifier;
entry
.pipe(etl.map(res => {
if (recordIdentifier) {
console.log(recordIdentifier);
console.log(i++);
// not sure about this. THis works but it only works for 1st file
// after that the program terminates. I need to do that for all the
// files in the zip
entry.destroy();
}
const data = res.toString("utf-8");
var array = data.toString().split("\n");
if(array.length >= 3) {
recordIdentifier = array[2].split(",")[0];
}
}))
})
}
setupMetadata();
我在阅读内容后尝试拨打entry.autodrain(),但它不起作用。 entry.destroy() 有效,但程序在此之后终止。我想对 zip 中的所有文件执行相同操作。
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
标签: node.js asynchronous unzip nodejs-stream