| Did this page help you? Yes No Tell us about it... |
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 |
2 | Execute the 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 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
|
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;
}