Failed to load PDF document using aws-sdk in Node.js

Viewed 4498

I have a problem when uploading pdf files to S3.

I have no problems creating pdf files using PDFkit and storing them locally (I can view them in my browser).

But when I try to upload them to my S3 storage, I get this error Failed to load PDF document. when opening a pdf in my browser. You can check it out: https://s3.us-east-2.amazonaws.com/pdf-twitter-bucket/twitter/HvkH98YA0p.pdf

        fs.readFile('../pdf/' + fileName, 'utf8', function (err, contents) {
          const buf = new Buffer(contents, 'base64');
          // define params
          const params = {
            Bucket: 'pdf-twitter-bucket',
            Key: 'twitter/' + fileName,
            Body: buf,
            ContentType: 'application/pdf',
            ACL: 'public-read'
          };

          s3bucket.upload(params, function (err) {
            if (err) {
              console.log('error in callback');
              console.log(err);
            }
            res.end(
              'https://s3.us-east-2.amazonaws.com/pdf-twitter-bucket/twitter/' +
              fileName
            ); // send the link to s3
          });
        });

I've tried many options: 1) using Buffeer and not using Buffer 2) I have no problems uploading .txt files 3) I've even tried passing a string as a Body value

Before writing this question I've carried out a small research and found similar problems:

https://github.com/VeliovGroup/Meteor-Files/issues/406

Unfortunately, it didn't help me much...

2 Answers
Related