如何使用 php 和 Amazon S3 sdk 下载文件? [英] How do I download a file with php and the Amazon S3 sdk?

查看:30
本文介绍了如何使用 php 和 Amazon S3 sdk 下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

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

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.

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

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

推荐答案

通过在回显 $object 主体之前回显内容类型标头使其工作.

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;

这篇关于如何使用 php 和 Amazon S3 sdk 下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆