使用nodejs aws sdk将生成的pdf上传到AWS S3 [英] Upload pdf generated to AWS S3 using nodejs aws sdk

查看:160
本文介绍了使用nodejs aws sdk将生成的pdf上传到AWS S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pdfkit生成包含一些自定义内容的pdf,然后将其发送到AWS S3存储桶。

I am using pdfkit to generate a pdf with some custom content and then sending it to an AWS S3 bucket.

如果我生成整个文件并且上传它完美的工作,但是,如果我想将生成的文件作为八位字节流流式传输,我无法找到任何相关的指针。

While if I generate the file as a whole and upload it works perfectly, however, if I want to stream the generated file probably as an octet stream I am not able to find any relevant pointers.

我正在寻找一个nodejs解决方案(或建议)。

I am looking for a nodejs solution (or suggestion).

推荐答案

我将在这里尽量准确。我将不会详细介绍 pdfKit 的nodejs sdk的使用情况。

I'll try to be precise here. I will not be covering usage of pdfKit's nodejs sdk in much detail.

如果您希望将生成的pdf作为文件。

IF you want your generated pdf as a file.

var PDFDocument = require('pdfkit');

// Create a document
doc = new PDFDocument();

// Pipe it's output somewhere, like to a file or HTTP response
doc.pipe(fs.createWriteStream('output.pdf'));
doc.text('Whatever content goes here');
doc.end();
var params = {
  key : fileName,
  body : './output.pdf',
  bucket : 'bucketName',
  contentType : 'application/pdf'
}

s3.putObject(params, function(err, response) {

});

但是如果你想要流式传输(在问题的上下文中说S3桶),那么它值得记住的是,每个pdfkit实例都是可读的流。

However if you want to stream it ( to say S3 bucket in the context of question), then it is worth remembering that every pdfkit instance is a readable stream.

S3期望文件,缓冲区或可读流。
所以,

And S3 expects a file, a buffer or a readable stream. So,

var doc = new PDFDocument();

// Pipe it's output somewhere, like to a file or HTTP response
doc.text("Text for your PDF");
doc.end();

var params = {
  key : fileName,
  body : doc,
  bucket : 'bucketName',
  contentType : 'application/pdf'
}

//notice use of the upload function, not the putObject function
s3.upload(params, function(err, response) {

});

这篇关于使用nodejs aws sdk将生成的pdf上传到AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆