[英]PDF uploading to AWS S3 corrupted
I managed to get my generated pdf uploaded to s3 from my node JS server. 我设法从节点JS服务器将生成的pdf上传到s3。 Pdf looks okay on my local folder but when I tried to access it from the AWS console, it indicates "Failed to load PDF document". Pdf在我的本地文件夹上看起来还可以,但是当我尝试从AWS控制台访问它时,它指示“无法加载PDF文档”。
I have tried uploading it via the s3.upload and s3.putObject APIs, (for the putObject I also used an .on finish checker to ensure that the file has been fully loaded before sending the request). 我尝试过通过s3.upload和s3.putObject API上载它(对于putObject,我还使用了.on完成检查器来确保在发送请求之前已完全加载文件)。 But the file in the S3 bucket is still the same (small) size, 26 bytes and cannot be loaded. 但是S3存储桶中的文件大小仍然相同(较小),为26个字节,无法加载。 Any help is greatly appreciated!!! 任何帮助是极大的赞赏!!!
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);
})
I realised that pdfDoc.end() must come before piping starts. 我意识到pdfDoc.end()必须在管道启动之前出现。 Have also used a callback to ensure that s3 upload is called after pdf write is finished. 还使用了回调函数来确保在pdf写入完成后调用s3上传。 See code below, hope it helps! 请参见下面的代码,希望对您有所帮助!
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);
});
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.