Home > OS >  Large pdf files are not opening in browser with aws signed url
Large pdf files are not opening in browser with aws signed url

Time:12-12

I am using NodeJS to upload a file to s3, and I am setting up the proper contentType for pdf while uploading. It looks something like this,

const params = {
  Bucket: "noob_bucket",
  Key: newFileName,
  Body: fs.createReadStream(path),
  ContentType: 'application/pdf',
  ACL: 'private',
};

now the problem is when I am trying to show the pdfon browser using signed URL it is opening in browser only if the file size is approximately less then 25MB, else it is simply downloading the file.

Can anyone help me how to fix this issue. I have files that is > 50MB as well.

Please help me to fix this. Thanks in advance

CodePudding user response:

The header which controls if a PDF file is displayed or downloaded as attachment is the content-disposition header. It should be inline if you want the content to be displayed in the browser.

You can set it explicitly in the parameters when you upload a file:

const params = {
  Bucket: "noob_bucket",
  Key: newFileName,
  Body: fs.createReadStream(path),
  ContentType: 'application/pdf',
  ContentDisposition: 'inline',
  ACL: 'private',
};

Also, you would want to set it when you request a presigned url:

const command = new GetObjectCommand({
    Bucket: 'noob_bucket',
    Key: newFileName,
    ResponseContentDisposition: 'inline',
    ResponseContentType: 'application/pdf',
});
const url = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
  • Related