简体   繁体   English

如何使用PHP将文件从签名的URL从Amazon S3下载到客户端计算机?

[英]How do I download a file from a signed URL from Amazon S3 to a client machine using PHP?

When a user clicks a link on the site, I'm trying to download a file from an Amazon S3 bucket with a signed URL through php. 当用户单击网站上的链接时,我正尝试通过php从具有签名URL的Amazon S3存储桶中下载文件。 Here's what I have so far: 这是我到目前为止的内容:

$client = S3Client::factory(
    array(
      'key' => 'xyz',
      'secret' => 'abc'
));

$signedUrl = $client->getObjectUrl(<bucket>, $location, '+10 minutes');

Now, how do I download the file using the above signed url to client machine? 现在,如何使用上面签名的URL将文件下载到客户端计算机?

Assuming that you are having problems only in fetching the url and have implemented api properly, this should work 假设您仅在获取url遇到问题并且已正确实现api,这应该可以工作

$client = S3Client::factory(
    array(
      'key' => 'xyz',
      'secret' => 'abc'
));

$signedUrl = $client->getObjectUrl(<bucket>, $location, '+10 minutes');
file_put_contents($fileName, file_get_contents($signedUrl));

Now this puts the file on your server. 现在,这会将文件放在您的服务器上。 In order to download to client there are a couple of methods. 为了下载到客户端,有两种方法。 Let me try with the most simplest 让我尝试最简单的方法

header('Content-Type: application/csv'); // Change mime type
header('Content-Disposition: attachment; filename=temp.csv'); // Change filename
header('Pragma: no-cache');
echo (file_get_contents($signedUrl));

This would rather than saving on server serve the output to client. 这将节省输出而不是在服务器上将输出提供给客户端。 Another alternative would be you send the url to javascript and download from client side. 另一种选择是将URL发送到javascript并从客户端下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM