Home > Back-end >  Display PDF file stored in S3 bucket
Display PDF file stored in S3 bucket

Time:02-26

Im using multer-s3 to upload a PDF to S3 bucket, and it works, but now I need to display the PDF without downloading it (just like this) and I already tryied to use iframe, embed and object, each one with the src='link_to_s3_file' and when I access the page it just download the file. That's my code to configure the upload:

const multer = require('multer')
const multerS3 = require('multer-s3')
const AWS = require('aws-sdk')

AWS.config.update({
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_REGION
})
const s3 = new AWS.S3()

const upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: process.env.AWS_S3_BUCKET,
        acl: 'private',
        contentDisposition: 'attachment',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname });
        },
        key: function (req, file, cb) {
            cb(null, file.originalname)
        }
    })
})

How can I display the PDF instead of download it?

CodePudding user response:

In order to open PDF directly, without downloading it, you should change 2 things:

ContentDisposition='inline'
ContentType='application/pdf'

If you're sure that multerS3.AUTO_CONTENT_TYPE will recognize it correctly, you might not have to change this, but ContentDisposition is for sure one thing you have to change. attachment means that it will download it and inline is to open it in browser directly.

CodePudding user response:

Try this:

<embed src="path_of_your_pdf/your_pdf_file.pdf" type="application/pdf"   height="700px" width="500">

You can define the type, which in your case is pdf.

  • Related