强制从 s3 亚马逊服务器下载

发布于 2024-11-07 06:06:59 字数 942 浏览 0 评论 0原文

我一直在开发一个新的 Web 应用程序,它依赖 Amazon S3 服务器作为存储系统,并使用 Codeiginter 作为 PHP 框架。

我需要在单击链接时强制下载文件。原始 URL 如下所示:

http://www.our-web.com/download/do/1.jpg

它会生成指向 Amazon S3 服务器上实际文件的临时签名 URL,如下所示:

http://main_bucket.s3.amazonaws.com/post/1/1.jpg?AWSAccessKeyId=AKIAJEOQKYPKC3CCU5RA&Expires=1305395426&Signature=iuzCdA22gImLK192%2BMAhk8OkAY8%3D

我需要让文件开始下载来自真实的亚马逊 URL用户单击该链接。

我现在有两种方法可以做到这一点:

  1. 使用 redirect() 这将打开文件而不是下载它;或
  2. 将标头更改为以下代码:

    header('内容类型:应用程序/强制下载');
    header('内容处置:附件;文件名=' . $file_name);
    header('内容传输编码:二进制');
    header('过期时间:4000');
    header('Cache-Control: 必须重新验证,后检查=0,预检查=0');
    标头('杂注:公共');
    header('内容长度:' .filesize($ generated_file));
    
    读取文件($生成的文件);
    

不幸的是,这两种方法都对我没有帮助。第二种方法导致下载来自我的网站,而不是直接来自亚马逊。

如何强制直接从 Amazon S3 服务器下载文件,而不是从我的网站下载?

I've been developing a new web application which relies on Amazon S3 servers as storage system, and Codeiginter as the PHP framework.

I need to force the file to download when the link is clicked. The original URL looks like this:

http://www.our-web.com/download/do/1.jpg

which generates a temporary signed URL to the actual file on the Amazon S3 servers like this:

http://main_bucket.s3.amazonaws.com/post/1/1.jpg?AWSAccessKeyId=AKIAJEOQKYPKC3CCU5RA&Expires=1305395426&Signature=iuzCdA22gImLK192%2BMAhk8OkAY8%3D

I need to make the file start downloading from the real Amazon URL it soon as the user clicks the link.

I have two ways now to do so:

  1. Use redirect() which will open the file not download it; or
  2. Alter headers as this code:

    header('Content-type: application/force-download');
    header('Content-Disposition: attachment; filename=' . $file_name);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 4000');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($generated_file));
    
    readfile($generated_file);
    

Unfortunately, both ways don't help me. The second method causes the download to come from my website and not from directly from Amazon.

How can I force the file to download directly from the Amazon S3 servers, and not from my website?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乖乖哒 2024-11-14 06:07:00

您只需在 S3 中的文件上设置正确的标头,即可强制浏览器下载而不是打开文件。设置这些:

Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream

将文件上传到 S3 时需要设置它们。通过 php SDK,您可以使用 create_object

或者,您可以在上传后使用 'change_content_type' 或通过 < a href="http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#m=AmazonS3/copy_object" rel="noreferrer">将文件复制到本身在S3中并设置正确的标头。

You just need to set the correct headers on your files in S3 in order to force the browser to download rather than opening the file. Set these:

Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream

You will need to set them when uploading the files to S3. With the php SDK you'd use create_object.

Or you can set these after uploading using 'change_content_type' or by copying the file to itself in S3 and setting the correct headers.

千纸鹤带着心事 2024-11-14 06:07:00

参加聚会有点晚了,但通常情况下,对于一个文件,您不想在存储时决定如何使用它。您希望能够存储该文件一次,然后在一个区域中可能嵌入该文件或在浏览器中显示,并在另一个区域中允许用户下载相同的文件。

幸运的是,您可以通过在请求 URL 中提供覆盖参数来做到这一点。它仅适用于签名的请求,但幸运的是您已经这样做了。

如果您添加像 &request-content-type="application/force-download" 这样的参数,应该可以解决问题。

查看 S3 GET 对象文档的请求参数部分:
http://docs.aws.amazon.com/AmazonS3/latest/API /RESTObjectGET.html

Bit late to the party, but often times with a file you don't want to have to decide at storage time how it will be used. You want to be able to store the file once, then in one area possibly embed the file or display in browser, and in another area enable the user to download the same file.

Fortunately you can do that by providing override parameters in the request url. It only works with signed requests, but thankfully you're already doing that.

If you add a parameter like &request-content-type="application/force-download" that should do the trick.

Check out the Request Parameters section of the S3 GET Object documentation:
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html

尤怨 2024-11-14 06:07:00

问题是以编程方式设置,但要通过 AWS 控制台手动设置:

  • 在 S3 中选择一个文件。
  • 属性>元数据>添加更多元数据
  • 键:Content-Disposition 值:附件
  • 保存

The question is about setting this programmatically, but to do so manually through the AWS console:

  • Select a file in S3.
  • Properties > Metadata > Add more metadata
  • Key: Content-Disposition Value: Attachment
  • Save
听你说爱我 2024-11-14 06:07:00

如果您的 S3 网站位于 CloudFront 之后,请不要忘记在重试之前使文件无效!这将清除缓存的文件。

您可以通过以下方式进入失效页面:

Cloudfront -> Distribution Settings -> Invalidation

然后输入文件的路径。

文件失效需要一些时间。我的文件花了 5 分钟。

If your S3 website is behind CloudFront, don't forget to invalidate the file before trying again! That will clear the cached file.

You can get to the invalidation page by going to:

Cloudfront -> Distribution Settings -> Invalidation

Then enter the path to the file.

It will take some time to invalidate the file. For my file it took 5 minutes.

醉殇 2024-11-14 06:07:00

您应该使用 $s3->getObject()ResponseContentDisposition 参数来设置响应的内容处置。您可以使用它来强制下载文件。

$s3->getObject([
  "Bucket" => "example",
  "Key" => "example.txt",
  "ResponseContentDisposition" => 'attachment; filename="example.txt"'
]);

You should use $s3->getObject()'s ResponseContentDisposition parameter to set the Content Disposition of the response. You can use this to force download a file.

$s3->getObject([
  "Bucket" => "example",
  "Key" => "example.txt",
  "ResponseContentDisposition" => 'attachment; filename="example.txt"'
]);
待天淡蓝洁白时 2024-11-14 06:07:00

您无法告诉浏览器如何处理远程文件。通过重定向到亚马逊,您告诉浏览器在那里开始一个新请求。您对该请求没有任何控制权。

我能想到的唯一解决方案是将图像打包成 zip 文件或类似文件。当然,这增加了另一种(可能令人讨厌的)形式的复杂性。

You can't tell the browser how to handle a remote file. By redirecting to amazon you're telling the browser to start a new request over there. You don't have any control over that request.

The only solution I can think of is to package the image into a zip file or similar. Of course that adds another (probably annoying) form of complexity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文