Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

node.js - Uploading PDF obtained from API directly to s3 in NodeJS

I am fetching a PDF report from a third party API, and I want to upload that PDF directly to the s3. When I do that, it uploads PDF to s3, but for some reason when I open that PDF in s3, all pages are blank. I am doing something wrong? My code is as below.

var report = reportInfo.body;
const params = {
   Key: 'report.pdf',
   Body: report,
   Bucket: process.env.S3_BUCKET_NAME,
   ContentType: 'application/pdf',
};
s3.upload(params, (err, res) => {
   if (err) {
        console.log(err, 'err');
   }
    console.log(res, 'res');
});

I am assigning the response from API to the report object. One part of the response is looking like this:

'%PDF-1.5 %???? 1 0 obj<</Length 2872/Filter/FlateDecode>>stream x???n$? ?????~?0? @?&1???>x?6?f????PU?U?a???mf???V?D?yHQ????~$gF7?????_/?????/??[????=??v?????F?u???S3?d??k??:O?????X?k???2?k????????????XY????Ti-3?y??y?u3?Q~???E??g?????f_I4??'>>>?$?&????e??G???0?1Go?@M??&?j??YJ3?zmhz??0<?Q??n???????i? 5w?0?1????O?5??SwM=?pm?????#f?>??q^g??j?J????}O?fi?xz&f?0?ǜ?^???yj???mm{?OM/B{z??%+??H??l4

I think that this is the plain PDF and that I can directly upload it to s3. Do I need to do something before uploading it?? Why it uploads only blank pages?

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Its all about encoding, In your scenario, file is the multipart file (pdf) that is passed to aws via a POST to the url.

  • server gets a file with this byte -> 0010 (this will not be interpreted right, because a standard byte has 8 bits)
  • so, we encode it in base 64 -> doesn't matter what result, decode it to get a standard byte -> 0000 0010 (now this is a standard byte and is interpreted right by aws)

For node.js encoding or decoding, you should refer this doc.

Another configuration which need's to be done, the API Gateway settings must be properly configured to support binary data types.

Path: AWS Console --> API Gateway --> Settings --> multipart/form-data

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...