如何在nodejs中压缩流?

How to gunzip stream in nodejs?

我正在尝试完成一项非常简单的任务,但我有点困惑,并坚持在 nodejs 中使用 zlib。我正在构建的功能包括我从 aws S3 下载文件,该文件是 gzip 压缩的,解压缩它并逐行读取它。我想使用流来完成所有这些,因为我相信在 nodejs 中可以这样做。

这是我当前的代码库:

//downloading zipped file from aws s3:
//params are configured correctly to access my aws s3 bucket and file

s3.getObject(params, function(err, data) {
  if (err) {
    console.log(err);
  } else {

    //trying to unzip received stream:
    //data.Body is a buffer from s3
    zlib.gunzip(data.Body, function(err, unzippedStream) {
      if (err) {
        console.log(err);
      } else {

        //reading line by line unzziped stream:
        var lineReader = readline.createInterface({
          input: unzippedStream
        });
        lineReader.on('line', function(lines) {
          console.log(lines);
        });
      }
    });
  }
});

我收到一条错误消息:

 readline.js:113

        input.on('data', ondata);
              ^

    TypeError: input.on is not a function

我认为解压缩过程中可能存在问题,但我不太确定哪里出了问题,如有任何帮助,我们将不胜感激。

我没有要测试的 S3 帐户,但 reading the docs 建议 s3.getObject() 可以 return 流,在这种情况下我认为这可能有效:

var lineReader = readline.createInterface({
  input: s3.getObject(params).pipe(zlib.createGunzip())
});
lineReader.on('line', function(lines) {
  console.log(lines);
});

编辑:看起来 API 可能已更改,现在您需要 instantiate a stream object manually 才能通过其他任何管道传输它:

s3.getObject(params).createReadStream().pipe(...)