![](/img/trans.png)
[英]How to pipe data modified from a gunzip stream into a gzip stream?
[英]How to gunzip stream in nodejs?
我正在努力完成一項非常簡單的任務,但我有點困惑,並堅持在nodejs中使用zlib。 我正在構建功能,包括我從aws S3下載文件gziped,解壓縮並逐行讀取。 我想用流完成所有這些,因為我相信它可以在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帳戶,但是閱讀文檔表明s3.getObject()
可以返回一個流,在這種情況下我認為這可能有效:
var lineReader = readline.createInterface({
input: s3.getObject(params).pipe(zlib.createGunzip())
});
lineReader.on('line', function(lines) {
console.log(lines);
});
編輯 :看起來API可能已更改,現在您需要手動實例化流對象,然后才能通過其他任何方式管道:
s3.getObject(params).createReadStream().pipe(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.