Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Get an Object Using the AWS SDK for PHP

The following tasks guide you through using the PHP classes to retrieve an object. You can retrieve an entire object or only a specific byte range from the object.

Downloading Files

1

Create an instance of the AmazonS3 class by providing your AWS credentials.

2

Execute the AmazonS3::get_object() method. You need to provide a bucket name, and a key name as parameters.

Instead of retrieving the entire object you can optionally retrieve a specific byte range from the object data. You provide the range value by specifying the optional array parameter with the range key.

You can save the object you retrieved from Amazon S3, to a file in your local file system by specifying the optional array parameter with the fileDownload key.


The following PHP code sample demonstrates the preceding tasks.

// Instantiate the class.
$s3 = new AmazonS3();
$response = $s3->get_object($bucket, $keyname);

// Get a range of bytes.
$response = $s3->get_object(
                     $bucket,
                     $keyname,
                     array('range'=> '0-10'));

// Save object to a file.
$response = $s3->get_object(
                    $bucket,
                    $keyname,
                    array('fileDownload'=> $filepath));

When retrieving an object, you can optionally override the response header values (see Getting Objects) by adding the optional response parameter to the get_object method, as shown in the following PHP code sample.

$response = $s3->get_object(
           $bucket,
           $keyname,
           array(
             'response' => array(
               'content-type'        => 'text/plain',
               'content-language'    => 'en-US',
               'content-disposition' => 'attachment; filename=testing.txt',
               'cache-control'       => 'No-cache',
               'expires' => gmdate(DATE_RFC2822, strtotime('1 January 1980'))
               )
         ));

Example

The following PHP example retrieves an object and displays object content in the browser. The example illustrates the use of the get_object() method.

<?php
require_once '../aws-sdk-for-php/sdk.class.php';

$bucket = '*** Provide bucket Name ***';
$keyname = '*** Provide object key name ***';
$filepath = '*** local file path to save the object ***';

// Instantiate the class.
$s3 = new AmazonS3();

// 1. Get object.
$response = $s3->get_object(
                      $bucket,
                      $keyname);

// Success?
if($response->isOK())
{
	header('Content-Type: ' . $response->header['content-type']);
	echo $response->body;
}