强制从s3亚马逊服务器下载 [英] Forcing Download from s3 amazon servers

查看:312
本文介绍了强制从s3亚马逊服务器下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个新的网络应用程序,它依赖于 Amazon S3 服务器作为存储系统, Codeiginter 作为PHP框架。

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

这将生成一个临时签名的URL到Amazon S3服务器上的实际文件,如下所示:

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

我需要让用户点击链接后立即从真实的Amazon网站开始下载文件。

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

我现在有两种方法:


  1. 使用 redirect()这将打开文件不下载它;
  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);


不幸的是, 。第二种方法导致下载来自我的网站,而不是直接来自Amazon。

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

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

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

推荐答案

您只需在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

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

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

或者您可以在上传后使用 'change_content_type'或通过在S3中将文件复制到自身并设置正确的标题。

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.

这篇关于强制从s3亚马逊服务器下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆