Vishesh Madhusudhana
Vishesh Madhusudhana

Reputation: 89

How to generate viewable s3 url nodejs

I use the below code to get the signed url. I am getting a signed url and once i open the url its downloading the file. But i want a url where i can show the file. Like opening file in a new tab.

I am using aws-sdk package and generate s3 as below

const s3 = new AWS.S3();

s3.getSignedUrl('getObject', {
            Expires: options.expires || 300,
            Bucket: config.awsBucket,
            Key: key,
            // ACL: 'public-read',
            ResponseContentDisposition :'inline;filename=report.pdf'}, 
          function(err, url) {
            if (err) {
                return deferred.reject(err);
            }
            return deferred.resolve(url);
        });

Does anyone know how to generate a url where i can show the file.

Upvotes: 1

Views: 2353

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179084

If you didn't set the Content-Type correctly when you uploaded the file, this behavior is to be expected, because the default type is binary/octet-stream, which the browser correctly determines cannot be displayed inline.

You can work around it by setting ResponseContentType in your getSignedUrl() request to the correct value.

The correct type for a PDF file is application/pdf.

Upvotes: 2

Related Questions