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

1

6

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.

Idol answered 19/8, 2018 at 23:28 Comment(0)
M
8

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.

Merciful answered 25/8, 2018 at 6:42 Comment(1)
Thanks; awesome answer!Idol

© 2022 - 2024 — McMap. All rights reserved.