1

I have the following code generating a presigned URL in my backend:

 response = s3.generate_presigned_url(
     'get_object',
     Params={'Bucket': 'myBucket','Key': filename},
     #Headers={'Content-Disposition': 'inline; filename='+filename},
     ExpiresIn=3600,
 )

When I visit this URL from my browser, it automatically downloads. I would like it to open in the browser window. Ideally, my web client would be able to choose this and use the same URL. In my research, I've found that I think I can have the HTML force a download where the URL is inline, by providing a download argument to the anchor tag, like so:

<a href="that_url">view</a>
<a href="that_url" download="filename">download</a>

However, given the code I have above, the default is to download and both of these links just download. I tried including some headers but those are not acceptable arguments to generate_presigned_url.

How do I get an inline URL from this function?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
mmachenry
  • 1,773
  • 3
  • 22
  • 38
  • 2
    I certainly agree. I also don't have an answer. There was an attempted answer you can see below but it didn't work for me and also didn't work for another commenter. Still a legitimately open question. I haven't solved this in my app yet. I've just removed the feature and have a task to get back to it one day. Now I'm noticing these a very promising looking answer that I need to try and possibly mark correct. – mmachenry Jan 03 '23 at 20:09

5 Answers5

0

For me it worked including the Content-Type

response = s3.generate_presigned_url(
    'get_object',
    Params={
        'Bucket': 'myBucket',
        'Key': filename,
        'ResponseContentType': 'application/pdf'
    }
)

Getting a pdf file, for instance.

0

For me this url was working and start showing inline instead for downloading. For .txt file -

client = boto3.client('s3')
client.generate_presigned_url(
            ClientMethod='get_object',
            Params={'Bucket': aws_bucket_name, 
                    'Key': object_key, 
                    "ResponseContentDisposition": "inline",
                    "ResponseContentType": "text/plain"},
        ExpiresIn=3600)
Aditi
  • 1
  • 1
  • 4
0

Inline is an OPTIONAL signal to the Browser (Users Web HTML editor) to simply say

IF the user settings are Download Only  
  THEN the download folder is to be determined by the user or their default,
  ELSE if the user HAS an inline PDF (or binary document) extension
    AND has set it to view a "Download" (cached or saved)
    THEN allow RETURN download to inline display.  

This applies in general to all non HTML attachments such as DjVu, XPS or previously ShockWaveFlash IF the user has such an object extender embedded in their browser AND "run as" is ALLOWED.

K J
  • 8,045
  • 3
  • 14
  • 36
0

There isn't away unless you generate 2 separate presigned urls with different ResponseContentDisposition

To Download file (example)

response = s3_client.generate_presigned_url('get_object',
                                                Params={'Bucket': bucket_name,
                                                        'Key': object_name,
                                                        'ResponseContentDisposition': 'attachment',
                                                        'ResponseContentType': 'video/mp4'},
                                                ExpiresIn=expiration)

To view file (example)

response = s3_client.generate_presigned_url('get_object',
                                                Params={'Bucket': bucket_name,
                                                        'Key': object_name,
                                                        'ResponseContentDisposition': 'inline',
                                                        'ResponseContentType': 'video/mp4'},
                                                ExpiresIn=expiration)

You can also check out solution in node.js

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 18:13
-2

This shall do the trick:

response = s3.generate_presigned_url(
    'get_object',
    Params={
        'Bucket': 'myBucket',
        'Key': filename,
        'ResponseContentDisposition': 'inline'
    }
)
jellycsc
  • 10,904
  • 2
  • 15
  • 32