amazonweb服务-如何使用PHP从AWS S3 Bucket下载对象


amazon web services - How to download an Object from AWS S3 Bucket with PHP?

我知道如何将对象上传到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…)并更改标头的内容类型,但这只是在浏览器上显示我的文件,但不下载文件。

tks!

可以使用s3client API 的getObject方法从s3 bucket下载文件

/**
 * 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

使用S3独立类(我发现它与AWS SDK没有太大区别)getObject有一个saveTo param,您可以在其中传递文件名以将文件保存到…查看方法:

/**
* 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

希望这能有所帮助。