Node js, piping pdfkit to a memory stream

后端 未结 5 1570
谎友^
谎友^ 2021-02-07 21:07

I am using pdfkit (https://github.com/devongovett/pdfkit) on my node server, typically creating pdf files, and then uploading them to s3. The problem is that pdfkit examples pip

5条回答
  •  长发绾君心
    2021-02-07 21:58

    There's no need to use an intermediate memory stream1 – just pipe the pdfkit output stream directly into a HTTP upload stream.

    In my experience, the AWS SDK is garbage when it comes to working with streams, so I usually use request.

    var upload = request({
        method: 'PUT',
        url: 'https://bucket.s3.amazonaws.com/doc.pdf',
        aws: { bucket: 'bucket', key: ..., secret: ... }
    });
    
    doc.pipe(upload);
    

    1 - in fact, it is usually undesirable to use a memory stream because that means buffering the entire thing in RAM, which is exactly what streams are supposed to avoid!

提交回复
热议问题