Johnny 5
Johnny 5

Reputation: 1551

Can I create a one-time S3 link w/ content-disposition: attachment?

I have PDFs, images, and text files in S3. I want the ability to create a download link to the file, but only sometimes. Sometimes my users want to view files on the web, but sometimes they want to click a "download" button to grab them in a single step (vs opening them and clicking the disk icon in the PDF viewer or hitting CTRL + S, etc.). For the latter case, I'd like to be able to generate a link that tells S3 what to do. It'd be nice if I could also control the name of the attachment, but I'd be more than happy to just be able to make an attachment.

Footnote

I am aware that you can add such headers to your files ahead of time, but this removes the ability for me to choose "download" or "view online" after the fact.

Upvotes: 6

Views: 1970

Answers (1)

isp-zax
isp-zax

Reputation: 3873

You can dynamically add headers to response based on the URL, by passing extra parameters response-content-disposition=attachment; filename='mycustomFileName' as described in the s3 Get Obejct docs.

That, however, only works for the authenticated (time limited) URLS, otherwise, user will receive InvalidRequest error similar to the following:

<Error>
<Code>InvalidRequest</Code>
<Message>
Request specific response headers cannot be used for anonymous GET requests.
</Message>
<RequestId>C5066C8E8F647EA2</RequestId>
<HostId>
ik5oj4QfcJX+5+F/PdootFuq47bh6hLpDqhbeyWlw+AADvMPpOPio6eYwXW7Fnx+H/zpVBM7DbA=
</HostId>
</Error>

If you want this to work with a non-authenticated users, download tag might be an option. As it described in this example, you can specify user-friendly filename instead of hash:

<a href="/images/490/736d910771bc3ee2.jpeg" download="some_nice_image_name.jpg">

although that clearly helps scrapers to automatically annotate your content.

Upvotes: 8

Related Questions