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

I'm trying to make it so that my script will show test.jpg in an Amazon S3 bucket through php. Here's what I have so far:

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;

This just dumps out the file data on the page. I think I need to also send the content-disposition header, which I thought was being done in the get_object() method, but it isn't.

Note: I'm using the SDK available here: http://aws.amazon.com/sdkforphp/

Got it to work by echo'ing out the content-type header before echo'ing the $object body.

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

I added the Content-Disposition header to the getAuthenticatedUrl();

 // Example
 $timeOut = 3600; // in seconds
 $videoName = "whateveryoulike";
 $headers = array("response-content-disposition"=>"attachment");
 $downloadURL = $s3->getAuthenticatedUrl( FBM_S3_BUCKET, $videoName, FBM_S3_LIFETIME + $timeOut, true, true, $headers );

Both of these methods work for me. The first way seems more concise.

    $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();

Or a more wordy but still functional way.

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

For PHP sdk3 change the last line of Maximus answer

    $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"];

If you're still looking for a relevant answer in 2019+, With AWS SDK for PHP 3.x and specifically '2006-03-01' with composer, the following worked for me


...

/**
 * 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>');

NOTE: This is a solution for those who would want to avoid the issues that may arise from proxying the download through their servers by using a direct download link from AWS.