【问题标题】:PDF uploading to AWS S3 corrupted上传到 AWS S3 的 PDF 已损坏
【发布时间】:2016-07-25 16:16:25
【问题描述】:

我设法将我生成的 pdf 从我的节点 JS 服务器上传到 s3。 Pdf 在我的本地文件夹上看起来不错,但是当我尝试从 AWS 控制台访问它时,它显示“无法加载 PDF 文档”。

我尝试通过 s3.upload 和 s3.putObject API 上传它(对于 putObject,我还使用了 .on 完成检查器来确保在发送请求之前文件已完全加载)。但是 S3 存储桶中的文件仍然是相同的(小)大小,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);
    })

【问题讨论】:

  • 在文本编辑器中打开文件并查看消息 - 很可能它告诉您错误
  • 我认为文件应该大于 26 字节。代码中的console.log(res); 行是否执行并打印任何内容?听起来您的应用在 s3.putObject() 调用完成之前就退出了。
  • Body: '/pdf/inspectionReport.pdf', string /pdf/inspectionReport.pdf 中有多少字节?看起来像 26。Body 期望 body -- 不是路径。
  • Michael-sqlbot 我认为你是对的。我用记事本在我的电脑中打开了下载文件的副本,它显示为“/pdf/inspectionReport.pdf”。但是,当我替换 Body: pdfDoc 的值时,AWS API 给了我一个错误 - [错误:无法确定 [object PDFDocument] 的长度]。我怎样才能传递pdf正文?另外,我想即使我的正文中只有“/pdf/inspectionReport.pdf”字符串,我也应该在我的 S3 存储桶中保存一个正确的 pdf 来读取该字符串而不是无法加载的文件。难道这完全是两个不同的问题?
  • @jlyh 你解决了这个问题吗??请与我们分享

标签: amazon-web-services amazon-s3 pdfkit corrupt pdfmake


【解决方案1】:

我意识到 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);
        });
    }
);

【讨论】:

    猜你喜欢
    • 2022-08-17
    • 2015-11-14
    • 2021-03-15
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    相关资源
    最近更新 更多