Ad

Nodejs Uploading Pdf To S3 Bucket Corrupt File

I am currently using aws-sdk to upload pdf files to bucket S3, like this:

function uploadFile (filePath, remoteFilename, cb) {
    var fileBuffer = fs.createReadStream(filePath); // ex.: 'temp/longFileName.pdf'
    fileBuffer.on('error', function(err) {
        logger.warn('Failed reading local pdf file');
        cb(err);
    });
    s3.upload({
        Bucket: 'someBucketName',
        Key: remoteFilename,
        Body: fileBuffer
    }, function (error, response) {
        cb(error, { response, remoteFilename });
    });
}

the problem is that sometimes the file gets uploaded with 0B size, sometimes it gets uploaded with the correct size, but when I download it, it is corrupt, and of course sometimes it is correctly uploaded and opens properly.

I read the pdf file locally from system file, and that pdf file is correct.

could somebody help me to fix this issue?

update

I am creating a pdf using pdfkit:

function createPdf (data, cb) {
    var fs = require('fs');
    var PDFDocument = require('pdfkit');
    var filePath = 'temp/longFileName.pdf';
    var pdf = new PDFDocument({
      size: 'LEGAL',
      info: {
        Title: 'Tile of File Here',
        Author: 'Some Author',
      }
    });

    // Write stuff into PDF
    pdf.text('Hello World');

    // Stream contents to a file
    pdf.pipe(
      fs.createWriteStream(filePath)
    )
      .on('finish', function () {
        console.log('PDF closed');
      });

    // Close PDF and write file.
    pdf.end();
    cb(null, {filePath})
}

once the callback in this function is called, i call the uploadFile function:

function doAll (someData, cb) {

    createPdf(someData, function(err, data) {
        if (err) console.log(err)

        uploadFile(data.filePath, function(err,data) {
            if (err) console.log(err)
            console.log('finished')
            cb(null, 'done');
            return;
        })
    })
}
Ad

Answer

The problem is that you're calling the callback immediately instead of waiting for the file to be fully written. Your callback function should be inside .on('finish')

pdf.pipe(
  fs.createWriteStream('./path/to/file.pdf')
)
.on('finish', function () {
    console.log('PDF closed');
    cb(null, 'finished'); // The callback should e in here
});

// Close PDF and write file.
pdf.end();
Ad
source: stackoverflow.com
Ad