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

Get an Object Using the AWS SDK for PHP

This topic explains how to use a class from the AWS SDK for PHP to retrieve an Amazon S3 object. You can retrieve an entire object or a byte range from the object. We assume 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.

When retrieving an object, you can optionally override the response header values by adding the response keys, ResponseContentType, ResponseContentLanguage, ResponseContentDisposition, ResponseCacheControl, and ResponseExpires, to the getObject() method, as shown in the following PHP code example:

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

For more information about retrieving objects, see Getting Objects.

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

<?php require 'vendor/autoload.php'; use Aws\S3\S3Client; use Aws\S3\Exception\S3Exception; $bucket = '*** Your Bucket Name ***'; $keyname = '*** Your Object Key ***'; $s3 = new S3Client([ 'version' => 'latest', 'region' => 'us-east-1' ]); try { // Get the object. $result = $s3->getObject([ 'Bucket' => $bucket, 'Key' => $keyname ]); // Display the object in the browser. header("Content-Type: {$result['ContentType']}"); echo $result['Body']; } catch (S3Exception $e) { echo $e->getMessage() . PHP_EOL; }

Related Resources

On this page: