简体   繁体   English

如何通过管道传递到作为缓冲区而非文件的Node.js流?

[英]How to pipe to a Node.js stream that's a buffer, not a file?

I've been searching everywhere, and all examples lead to fs.createWriteStream which is not what I want. 我到处都在搜索,所有示例都导致了fs.createWriteStream ,这不是我想要的。

I'm using the archiver package, and would like to use archive.pipe() to pipe to a writeable stream that is not a file, but rather a buffer that I can use to send to s3.putObject to write to an S3 bucket. 我使用的是archiver包,并且想使用archive.pipe()传递到不是文件的可写流,而是一个可用于发送到s3.putObject以写入S3存储桶。 How do I set up a buffer that I can pipe to? 如何设置可以通过管道传输的缓冲区?

When I run the code below, I get "Error: not implemented". 当我运行下面的代码时,出现“错误:未实现”。

const stream = require('stream');
const archiver = require('archiver');

const archive = archiver('zip');
const outputStream = new stream.Writable();
outputStream.on('close', () => {
  console.log('done');
});
outputStream.on('error', err => {
  console.error(err);
});
archive.pipe(outputStream);
archive.append('Testing 1 2 3', { name: 'file1.txt' });
archive.finalize();

Got it! 得到它了!

const bl = require('bl');
const AWS = require('aws-sdk');
const archiver = require('archiver');

const s3 = new AWS.S3();
const archive = archiver('zip');
let buf;

archive.pipe(
  bl((err, data) => {
    buf = data;
  })
);
archive.file('whoa.wav', { name: 'file1.wav' });
archive.finalize().then(x => {
  console.log('finalized', buf);
  s3.putObject(
    {
      Bucket: 'xyz',
      Key: 'whatever.zip',
      Body: buf,
    },
    err => {
      console.log('s3 result:', err);
    }
  );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM