[英]Archiver combine/pipe with PassThrough(), append and finalize not a function - Nodejs Stream
So, I am downloading files with the help of Axios stream, zipping them with archiver then I want to upload the zip to s3 bucket.所以,我在 Axios stream 的帮助下下载文件,用归档器压缩它们然后我想将 zip 上传到 s3 存储桶。 At first, I saved zip in local directory, everything worked fine like this.
起初,我将 zip 保存在本地目录中,一切正常。 I used multipipe lib to combine streams.
我使用多管道库来组合流。 Combine archiver zip stream with fs.createWriteStream
将归档器 zip stream 与 fs.createWriteStream 组合
function uploadFromStream(name) {
const output = fs.createWriteStream(__dirname + `/${name}.zip`);
const zip= combine(archive, output);
return zip
}
At the axios end在 axios 端
zip.append(response.data.pipe(new PassThrough()), { name: name });
Everything worked as expected, Zip is getting saved with all files in it.一切都按预期工作,Zip 正在保存其中的所有文件。
Then I researched and look for how to upload to s3 with stream.然后我研究并寻找如何使用 stream 上传到 s3。 I found this way.
我找到了这种方式。 Pipe a stream to s3.upload()
Pipe 一个 stream 到 s3.upload()
inputStream.pipe(uploadFromStream(s3)); inputStream.pipe(uploadFromStream(s3));
function uploadFromStream(s3) {
var pass = new stream.PassThrough();
var params = {Bucket: BUCKET, Key: KEY, Body: pass};
s3.upload(params, function(err, data) {
console.log(err, data);
});
return pass;
}
I implemented this with archiver like this.我用这样的归档器实现了这个。
function uploadFromStream(bucket, destination) {
var pass = new stream.PassThrough();
var params = { Bucket: bucket, Key: destination, Body: pass };
S3.upload(params, function (err, data) {
console.log(err, data);
});
const s3Stream = combine(archive, pass);
return s3Stream;
}
Now when I try to append I am getting error append not a function same when finalize is not a function.现在,当我尝试 append 时,我收到错误 append not a function 相同时 finalize 不是 ZC15C41452674C163。 When I check I found that s3Stream.append and s3Stream.finalize are undefined.
当我检查时,我发现 s3Stream.append 和 s3Stream.finalize 是未定义的。 I dont think It should happen but I guess PassThrough is removing functions from combined stream.
我不认为它应该发生,但我猜 PassThrough 正在从组合 stream 中删除功能。
And piping streams like this also did not work.像这样的管道流也不起作用。
return archive.pipe(pass);
So after logging streams many times.所以在多次记录流之后。 I think pipe does not combine functions of two streams, but only creates a flow between two streams.
我认为 pipe 没有结合两个流的功能,而只是在两个流之间创建一个流。 So I just need to pipe passThrough and archiver then return archiver to append files to it.
所以我只需要 pipe passThrough 和归档器然后将归档器返回到 append 文件给它。
function uploadFromStream(bucket, destination) {
const pass = new stream.PassThrough();
const params = { Bucket: bucket, Key: destination, Body: archive };
S3.upload(params, function (err, data) {
console.log(err, data);
});
archive.pipe(pass);
return archive;
}
By doing this I am not facing the above mentioned error.通过这样做,我不会面临上述错误。 My bad was working with streams first time
我的坏处是第一次使用流
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.