我可以创建 one-time S3 link w/ content-disposition: 附件吗?

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

我在 S3 中有 PDF、图像和文本文件。我希望能够创建文件的下载 link,但仅限于某些时候。有时我的用户想在网络上查看文件,但有时他们想单击 "download" 按钮一步抓取它们(与打开它们并单击 PDF 查看器中的磁盘图标或按 CTRL + S, ETC。)。对于后一种情况,我希望能够生成一个 link 来告诉 S3 做什么。如果我也能控制附件的名称就好了,但我更乐意能做一个附件。

脚注

我知道您可以提前将这样的 headers 添加到您的文件中,但这使我无法在事后选择 "download" 或 "view online"。

你可以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.

然而,这仅适用于 authenticated (time limited) URLS,否则,用户将收到 InvalidRequest 类似于以下内容的错误:

<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>

如果您希望它与未经身份验证的用户一起使用,download tag might be an option. As it described in this example,您可以指定用户友好的文件名而不是哈希:

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

虽然这显然有助于爬虫自动注释您的内容。