【问题标题】:Streaming uploaded file to s3 using formidable使用强大的流式上传文件到 s3
【发布时间】:2020-05-14 02:46:42
【问题描述】:

我知道这个问题已经被问过很多次了,但我仍然无法让它发挥作用, 我正在使用强大的解析传入文件,而不是将文件存储在内存中,我想流式传输到 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);
};

由于我为每个chunk 调用s3.upload,它会覆盖前一个数据块,那么我该如何处理到 s3 的流?

【问题讨论】:

    标签: node.js express amazon-s3 formidable node-streams


    【解决方案1】:

    我解决了这个问题,如下所示。

    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);
      };
    

    【讨论】:

    • 我不认为这会起作用,因为 data 是一个缓冲区,所以 data.pipe 将是未定义的。
    猜你喜欢
    • 2013-06-22
    • 2021-09-23
    • 2020-04-02
    • 2016-03-14
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2016-12-29
    相关资源
    最近更新 更多