我知道如何将对象上传到Aws S3 Bucket:
try {
$oClientAws->putObject(array(
'Bucket' => 'bucket_test',
'Key' => 'fileName.jpg',
'Body' => fopen('path/to/file/fileName.jpg', 'r'),
'ACL' => 'public-read',
));
}
catch (Aws\Exception\S3Exception $e) {}
但我不知道如何下载一个对象,我可以使用$ oClientAws-> getObject(parms ...)并更改标题的内容类型,但这只是在浏览器上显示我的文件,但不要' t下载文件。
TKS!
答案 0 :(得分:2)
可以使用s3client API的getObject方法从s3存储桶下载文件
/**
* Gets file stored in Amazon s3 bucket.
*
* @param string $awsAccesskey , access key used to access the Amazon bucket.
* @param string $awsSecretKey , secret access key used to access the Amazon bucket.
* @param string $bucketName , bucket name from which the file is to be accessed.
* @param string $file_to_fetch , name of the file to be fetched, if the file is with in a folder it should also include the folder name.
* @param string $path_to_save , path where the file received should be saved.
* @return boolean true if the file is successfully received and saved else returns false.
*/
function get_from_s3_bucket( $awsAccesskey, $awsSecretKey, $bucketName, $file_to_fetch, $path_to_save ) {
try {
$bucket = $bucketName;
require_once('S3.php');
$s3 = new S3( $awsAccesskey, $awsSecretKey );
$object = $s3->getObject( $bucket, $file_to_fetch, $path_to_save );
if ( $object->code == 200 ) {
return true;
} else {
return false;
}
} catch ( Exception $e ) {
return false;
}
}
请参阅以下链接以获取更多指导:
http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html
答案 1 :(得分:1)
使用S3独立类(我发现它与AWS SDK没什么不同)getObject有一个saveTo参数,你传递一个文件名来保存文件...检查方法:
/**
* Get an object
*
* @param string $bucket Bucket name
* @param string $uri Object URI
* @param mixed $saveTo Filename or resource to write to
* @return mixed
*/
public static function getObject($bucket, $uri, $saveTo = false)
{
$rest = new S3Request('GET', $bucket, $uri, self::$endpoint);
if ($saveTo !== false)
{
if (is_resource($saveTo))
$rest->fp =& $saveTo;
else
if (($rest->fp = @fopen($saveTo, 'wb')) !== false)
$rest->file = realpath($saveTo);
else
$rest->response->error = array('code' => 0, 'message' => 'Unable to open save file for writing: '.$saveTo);
}
if ($rest->response->error === false) $rest->getResponse();
if ($rest->response->error === false && $rest->response->code !== 200)
$rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status');
if ($rest->response->error !== false)
{
self::__triggerError(sprintf("S3::getObject({$bucket}, {$uri}): [%s] %s",
$rest->response->error['code'], $rest->response->error['message']), __FILE__, __LINE__);
return false;
}
return $rest->response;
}
这是获取课程的链接:https://aws.amazon.com/code/1448
希望这有帮助。