加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-07-06 05:20:31 所属栏目:大数据 来源: https://www.jb51.cc
导读:我正在努力使我的脚本通过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_ob

我正在努力使我的脚本通过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;

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

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

解决方法:

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

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

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

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

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读