如何使用php和Amazon S3 sdk下载文件?

dor*_*emi 13 php amazon-s3

我正在努力使我的脚本通过php在Amazon S3存储桶中显示test.jpg.这是我到目前为止所拥有的:

require_once('library/AWS/sdk.class.php');

$s3 = new AmazonS3($key, $secret);

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg', array('headers' => array('content-disposition' => $objInfo->header['_info']['content_type'])));

echo $obj->body;
Run Code Online (Sandbox Code Playgroud)

这只是转储页面上的文件数据.我想我还需要发送content-disposition头文件,我认为这是在get_object()方法中完成的,但事实并非如此.

注意:我正在使用此处提供的SDK:http://aws.amazon.com/sdkforphp/

Max*_*mus 18

这两种方法都适合我.第一种方式似乎更简洁.

    $command = $s3->getCommand('GetObject', array(
       'Bucket' => 'bucket_name',
       'Key'    => 'object_name_in_s3'  
       'ResponseContentDisposition' => 'attachment; filename="'.$my_file_name.'"'
    ));

    $signedUrl = $command->createPresignedUrl('+15 minutes');
    echo $signedUrl;
    header('Location: '.$signedUrl);
    die();
Run Code Online (Sandbox Code Playgroud)

或者更罗嗦但仍然功能性的方式.

    $object = $s3->getObject(array(
    'Bucket' => 'bucket_name',
    'Key'    => 'object_name_in_s3'   
    ));

    header('Content-Description: File Transfer');
    //this assumes content type is set when uploading the file.
    header('Content-Type: ' . $object->ContentType);
    header('Content-Disposition: attachment; filename=' . $my_file_name);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //send file to browser for download. 
    echo $object->body;
Run Code Online (Sandbox Code Playgroud)


dor*_*emi 13

通过在回显$ object体之前回显出内容类型标题来实现它.

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg');

header('Content-type: ' . $objInfo->header['_info']['content_type']);
echo $obj->body;
Run Code Online (Sandbox Code Playgroud)

  • 使用“echo $obj->body”时要小心,较大的文件可能会耗尽内存!更好的方法是使用 `$response['Body']->rewind();` while ($data = $response['Body']->read(1024)) { echo $data;}` 参考: https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#downloading-objects (2认同)

Raj*_*rma 5

对于 PHP sdk3 更改 Maximus 答案的最后一行

    $object = $s3->getObject(array(
       'Bucket' => 'bucket_name',
       'Key'    => 'object_name_in_s3'   
    ));

    header('Content-Description: File Transfer');
    //this assumes content type is set when uploading the file.
    header('Content-Type: ' . $object->ContentType);
    header('Content-Disposition: attachment; filename=' . $my_file_name);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //send file to browser for download. 
    echo $object["Body"];
Run Code Online (Sandbox Code Playgroud)


Nat*_*ush 5

如果您仍在寻找 2019 年以后的相关答案,使用适用于 PHP 3.x 的 AWS SDK,特别是使用 Composer 的“2006-03-01”,以下内容对我有用


...

/**
 * Download a file
 * 
 * @param   string  $object_key
 * @param   string  $file_name
 * @return  void
 */
function download($object_key, $file_name = '') {
  if ( empty($file_name) ) {
    $file_name = basename($file_path);
  }
  $cmd = $s3->getCommand('GetObject', [
    'Bucket'                        => '<aws bucket name>',
    'Key'                           => $object_key,
    'ResponseContentDisposition'    => "attachment; filename=\"{$file_name}\"",
  ]);
  $signed_url = $s3->createPresignedRequest($cmd, '+15 minutes') // \GuzzleHttp\Psr7\Request
                ->getUri() // \GuzzleHttp\Psr7\Uri
                ->__toString();
  header("Location: {$signed_url}");
}
download('<object key here>', '<file name for download>');
Run Code Online (Sandbox Code Playgroud)

注意:对于那些希望避免使用来自 AWS 的直接下载链接通过服务器代理下载可能出现的问题的人来说,这是一个解决方案。

  • 感谢您的简洁回答。遗憾的是 AWS 没有在其文档中提供类似这样的可用快速方法。另外“...这可能是由于通过其服务器代理下载而引起的”...如果您提供 800MB 文件,则可能会出现内存不足的情况。这不像是发生在我身上或其他什么事情。奥奥 (2认同)