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...

Listing Keys Using the AWS SDK for PHP

When enumerating objects in a bucket you have option of using the low-level list_objects() method or the high-level get_object_list() method.

The low-level list_objects() method maps to the underlying Amazon S3 REST API. Each list_objects() request returns you a page of up to 1,000 object keys. If you have more than 1,000 objects in the bucket, your response will be truncated. You will need to send another list_objects() request to fetch the next set of 1,000 keys. This method returns the entire Amazon S3 response. For example, in addition to key names, the response includes object metadata such as content size and the last modified date.

The high-level get_object_list() method provides higher level of abstraction which simplifies your coding efforts. For example, you don't need to worry if the response is truncated or not. The method returns all the keys from the specified bucket. Internally, this API makes all the necessary calls to ensure all the keys are retrieved. However, the API returns only object keys names in an array. You use this high-level API is to enumerate object keys from a specific bucket.

The following tasks guide you through using the PHP classes to list object keys in a bucket.

Listing Object Keys

1

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

2

Execute the high-level AmazonS3::get_object_list() or the low-level AmazonS3::list_objects() method by providing the bucket name as input.


The following PHP code sample demonstrates the preceding tasks.

// Instantiate the class.
$s3 = new AmazonS3();
// Use the high-level API.
$response = $s3->get_object_list($bucket);

// Or, use the low-level API().
$response = $s3->list_objects($bucket);
// Process response. 

Example

The following PHP example lists keys from a specified bucket. The example illustrates the use of the both the high-level get_object_list() method and the low-level list_objects() method to enumerate objects in a bucket. The example also illustrates processing the response returned by these methods.

<?php
require_once '../aws-sdk-for-php/sdk.class.php';
header('Content-Type: text/plain; charset=utf-8');

$bucket = *** Provide bucket name ***';


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

//   1. High-level API returns all keys. However only key names are returned.

$response = $s3->get_object_list($bucket);

// Success?
print_r(gettype($response) === 'array');

if($response)
{
   echo "Keys retrieved!" . PHP_EOL;

	foreach ($response as $key)
    {
        echo $key . PHP_EOL;
    }
}

/*
    2. Low-level API. Response returns up to 1,000 keys.
       However, response has more information than just key names.
*/

$response = $s3->list_objects($bucket);

foreach ($response->body->Contents as $content)
{
   echo (string) $content->Key . ' - ' .
        $s3->util->size_readable((string) $content->Size) . ' - ' .
        date('j M Y', strtotime((string) $content->LastModified)) . PHP_EOL;
}