使用强大功能将上传的文件流式传输到s3

问题描述 投票:0回答:1

我知道这个问题已被问过多次,但我仍然无法解决这个问题,我正在使用强大的解析传入文件,而不是将文件存储在内存中,我想流式传输到s3。

我的请求处理程序如下所示。

profile = async (req: Request) => {
    const form = new IncomingForm();
    form.onPart = (part: Part) => {
      part.on("data", function(data){
          // here calling s3 upload for each chunk
          s3.upload({Body: data, Key: 'test.jpg'})
      })
    };

    form.parse(req);
};

由于我为每个s3.upload都调用chunk,所以它将覆盖先前的数据块,所以我如何处理到s3的流?

node.js express amazon-s3 formidable node-streams
1个回答
0
投票

我像下面那样解决了这个问题。

profile = async (req: Request) => {
    const form = new IncomingForm();
    const passThrough = new PassThrough();
    const promise = s3.upload({Bucket: 'my-test-bucket', Key: 'unique.jpg', Body: 
    passThrough}).promise()
    form.onPart = (part: Part) => {
        part.on("data", function(data){
           // pass chunk to passThrough
           data.pipe(passThrough)
        })
    };

     promise.then(uploadedData => console.log(uploadedData)).catch(err => console.log(err))

     form.parse(req);
  };
© www.soinside.com 2019 - 2024. All rights reserved.