Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Get an Object Using the AWS SDK for PHP

This topic guides you through using a class from the AWS SDK for PHP to retrieve an object. You can retrieve an entire object or specify a byte range to retrieve from the object.


This topic assumes that you are already following the instructions for Using the AWS SDK for PHP and Running PHP Examples and have the AWS SDK for PHP properly installed.

Downloading an Object


Create an instance of an Amazon S3 client by using the Aws\S3\S3Client class factory() method.


Execute the Aws\S3\S3Client::getObject() method. You must provide a bucket name and a key name in the array parameter's required keys, Bucket and Key.

Instead of retrieving the entire object you can retrieve a specific byte range from the object data. You provide the range value by specifying the array parameter's Range key in addition to the required keys.

You can save the object you retrieved from Amazon S3 to a file in your local file system by specifying a file path to where to save the file in the array parameter's SaveAs key, in addition to the required keys, Bucket and Key.

The following PHP code example demonstrates the preceding tasks for downloading an object.

use Aws\S3\S3Client; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; $filepath = '*** Your File Path ***'; // Instantiate the client. $s3 = S3Client::factory(); // Get an object. $result = $s3->getObject(array( 'Bucket' => $bucket, 'Key' => $keyname )); // Get a range of bytes from an object. $result = $s3->getObject(array( 'Bucket' => $bucket, 'Key' => $keyname, 'Range' => 'bytes=0-99' )); // Save object to a file. $result = $s3->getObject(array( 'Bucket' => $bucket, 'Key' => $keyname, 'SaveAs' => $filepath ));

When retrieving an object, you can optionally override the response header values (see Getting Objects) by adding the array parameter's response keys, ResponseContentType, ResponseContentLanguage, ResponseContentDisposition, ResponseCacheControl, and ResponseExpires, to the getObject() method, as shown in the following PHP code example.

$result = $s3->getObject(array( 'Bucket' => $bucket, 'Key' => $keyname, 'ResponseContentType' => 'text/plain', 'ResponseContentLanguage' => 'en-US', 'ResponseContentDisposition' => 'attachment; filename=testing.txt', 'ResponseCacheControl' => 'No-cache', 'ResponseExpires' => gmdate(DATE_RFC2822, time() + 3600), ));

Example of Downloading an Object Using PHP

The following PHP example retrieves an object and displays object content in the browser. The example illustrates the use of the getObject() method. For information about running the PHP examples in this guide, go to Running PHP Examples.

<?php // Include the AWS SDK using the Composer autoloader. require 'vendor/autoload.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; // Instantiate the client. $s3 = S3Client::factory(); try { // Get the object $result = $s3->getObject(array( 'Bucket' => $bucket, 'Key' => $keyname )); // Display the object in the browser header("Content-Type: {$result['ContentType']}"); echo $result['Body']; } catch (S3Exception $e) { echo $e->getMessage() . "\n"; }

Related Resources

On this page: