我将尝试完成一个非常简单的任务,但我在使用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
我认为问题可能出在解压过程中,但我不确定出了什么问题,希望能得到帮助。
.createReadStream()
,如s3.getObject(params).createReadStream().pipe(zlib.createGunzip())
,它完美地工作了。现在我只需要加入一些错误处理,非常感谢您! - Tomass3.getObject(...).createReadStream().pipe()
- robertklep