簡體   English   中英

使用nodejs aws sdk 將生成的pdf上傳到AWS S3

[英]Upload pdf generated to AWS S3 using nodejs aws sdk

我正在使用 pdfkit 生成帶有一些自定義內容的 pdf,然后將其發送到 AWS S3 存儲桶。

雖然如果我生成整個文件並上傳它完美無缺,但是,如果我想 stream 生成的文件可能作為八位字節 stream 我找不到任何相關的指針。

我正在尋找 nodejs 解決方案(或建議)。

我會在這里盡量准確。 我不會詳細介紹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) {

});

如果您使用 html-pdf 包和 aws-sdk 比它很容易...

var pdf = require('html-pdf');
import aws from 'aws-sdk';
const s3 = new aws.S3();

pdf.create(html).toStream(function(err, stream){
  stream.pipe(fs.createWriteStream('foo.pdf'));
  const params = {
                Key: 'foo.pdf',
                Body: stream,
                Bucket: 'Bucket Name',
                ContentType: 'application/pdf',
            };
  s3.upload(params, (err, res) => {
                if (err) {
                    console.log(err, 'err');
                }
                console.log(res, 'res');
            });
});

試過這個並工作。 我創建了一個 readFileSync,然后將其上傳到 S3。 我還使用了“writeStream.on('finish'”,以便在上傳之前完全創建pdf文件,否則上傳部分文件。

const PDFDocument = require('pdfkit');
const fs = require('fs');
const AWS = require('aws-sdk');
const path = require('path')

async function createPDF() {


const doc = new PDFDocument({size: 'A4'});
let writeStream = fs.createWriteStream('./output.pdf')
doc.pipe(writeStream);


// Finalize PDF file
doc.end();

writeStream.on('finish', function () {
    var appDir = path.dirname(require.main.filename);
    const fileContent = fs.readFileSync(appDir + '/output.pdf');
    var params = {
        Key : 'filName',
        Body : fileContent,
        Bucket : process.env.AWS_BUCKET,
        ContentType : 'application/pdf',
        ACL: "public-read"
      }
      
    const s3 = new AWS.S3({
        accessKeyId: process.env.AWS_ACCESS_KEY,
        secretAccessKey: process.env.AWS_SECRET_KEY
    });
      //notice use of the upload function, not the putObject function
    s3.upload(params, function(err, response) {
        
    });
});

}

這就是我將帶有 Cypress 的 PDF 文件上傳到 AWS S3 的方式:

cy.readFile("cypress/fixtures/Document.pdf", "base64").then((fileBase64) => {
    const fileBlob = Cypress.Blob.base64StringToBlob(fileBase64, "application/pdf");
    uploadFileToS3(blob);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM