我正在努力使我的脚本通过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)
对于 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)
如果您仍在寻找 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 的直接下载链接通过服务器代理下载可能出现的问题的人来说,这是一个解决方案。
| 归档时间: |
|
| 查看次数: |
34049 次 |
| 最近记录: |