PDF 上传到 AWS S3 损坏

PDF uploading to AWS S3 corrupted

我设法将我生成的 pdf 从我的节点 JS 服务器上传到 s3。 Pdf 在我的本地文件夹中看起来没问题,但是当我尝试从 AWS 控制台访问它时,它指示 "Failed to load PDF document".

我尝试通过 s3.upload 和 s3.putObject API 上传它,(对于 putObject 我还使用了 .on 完成检查器以确保在发送请求之前文件已完全加载).但是S3 bucket中的文件还是一样(小)大小,26字节,无法加载。非常感谢任何帮助!!!

    var pdfDoc = printer.createPdfKitDocument(inspectionReport);
    var writeStream = fs.createWriteStream('pdfs/inspectionReport.pdf');
    pdfDoc.pipe(writeStream);
    pdfDoc.end();
    writeStream.on('finish', function(){
        const s3 = new aws.S3();
        aws.config.loadFromPath('./modules/awsconfig.json');

    var s3Params = {
        Bucket: S3_BUCKET,
        Key: 'insp_report_test.pdf',
        Body: '/pdf/inspectionReport.pdf',
        Expires: 60,
        ContentType: 'application/pdf'
    };
    s3.putObject(s3Params, function(err,res){
        if(err) 
            console.log(err);
        else
            console.log(res);
    })

我意识到 pdfDoc.end() 必须在管道开始之前出现。还使用回调来确保在 pdf 写入完成后调用 s3 上传。请参阅下面的代码,希望对您有所帮助!

var pdfDoc = printer.createPdfKitDocument(inspectionReport);
pdfDoc.end();    

async.parallel([

        function(callback){
            var writeStream = fs.createWriteStream('pdfs/inspectionReport.pdf');
            pdfDoc.pipe(writeStream);
            console.log('pdf write finished!');
            callback();
        }

    ], function(err){

        const s3 = new aws.S3();
        var s3Params = {
            Bucket: S3_BUCKET,
            Key: 'insp_report_test.pdf',
            Body: pdfDoc,
            Expires: 60,
            ContentType: 'application/pdf'
        };

        s3.upload(s3Params, function(err,result){
            if(err) console.log(err);
            else console.log(result);
        });
    }
);