简体   繁体   中英

How to download an Object from AWS S3 Bucket with PHP?

I know how do upload an object to Aws S3 Bucket like this:

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) {}

But i don't know how to download an object i can use $oClientAws->getObject(parms...) and change the content type of the header but this just show my file on the browser, but don't download the file.

tks!

File can be downloaded from s3 bucket using getObject method of s3client API

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

Refer the below link for more guidance:

http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html

Using the S3 standalone class (which I have found isn't much different from the AWS SDK) getObject has a saveTo param where you pass a filename to save the file to... Check out the method:

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

Here's a link to obtain the class: https://aws.amazon.com/code/1448

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM