使用 nodejs aws sdk 将生成的 pdf 上传到 AWS S3

新手上路,请多包涵

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

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

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

原文由 Shivendra Soni 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 383
1 个回答

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

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

 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 实例都是一个可读流。

S3 需要一个文件、一个缓冲区或一个可读流。所以,

 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) {

});

原文由 Shivendra Soni 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题