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

Listing Keys Using the AWS SDK for PHP

This topic guides you through using classes from the AWS SDK for PHP to list the object keys contained in an Amazon S3 bucket.

Note

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.

To list the object keys contained in a bucket using the AWS SDK for PHP you first must list the objects contained in the bucket and then extract the key from each of the listed objects. When listing objects in a bucket you have the option of using the low-level Aws\S3\S3Client::listObjects() method or the high-level Aws\S3\Iterator\ListObjects iterator.

The low-level listObjects() method maps to the underlying Amazon S3 REST API. Each listObjects() request returns a page of up to 1,000 objects. If you have more than 1,000 objects in the bucket, your response will be truncated and you will need to send another listObjects() request to retrieve the next set of 1,000 objects.

You can use the high-level ListObjects iterator to make your task of listing the objects contained in a bucket a bit easier. To use the ListObjects iterator to create a list of objects you execute the Amazon S3 client getIterator() method that is inherited from Guzzle\Service\Client class with the ListObjects command as the first argument and an array to contain the returned objects from the specified bucket as the second argument. When used as a ListObjects iterator the getIterator() method returns all the objects contained in the specified bucket. There is no 1,000 object limit, so you don't need to worry if the response is truncated or not.

The following tasks guide you through using the PHP Amazon S3 client methods to list the objects contained in a bucket from which you can list the object keys.

Listing Object Keys

1

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

2

Execute the high-level Amazon S3 client getIterator() method with the ListObjects command as the first argument and an array to contain the returned objects from the specified bucket as the second argument.

Or you can execute the low-level Amazon S3 client listObjects() method with an array to contain the returned objects from the specified bucket as the argument.

3

Extract the object key from each object in the list of returned objects.

The following PHP code sample demonstrates how to list the objects contained in a bucket from which you can list the object keys.

Copy
use Aws\S3\S3Client; // Instantiate the client. $s3 = S3Client::factory(); $bucket = '*** Bucket Name ***'; // Use the high-level iterators (returns ALL of your objects). $objects = $s3->getIterator('ListObjects', array('Bucket' => $bucket)); echo "Keys retrieved!\n"; foreach ($objects as $object) { echo $object['Key'] . "\n"; } // Use the plain API (returns ONLY up to 1000 of your objects). $result = $s3->listObjects(array('Bucket' => $bucket)); echo "Keys retrieved!\n"; foreach ($result['Contents'] as $object) { echo $object['Key'] . "\n"; }

Example of Listing Object Keys

The following PHP example demonstrates how to list the keys from a specified bucket. It shows how to use the high-level getIterator() method to list the objects in a bucket and then how to extract the key from each of the objects in the list. It also show how to use the low-level listObjects() method to list the objects in a bucket and then how to extract the key from each of the objects in the list returned. For information about running the PHP examples in this guide, go to Running PHP Examples.

Copy
<?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 ***'; // Instantiate the client. $s3 = S3Client::factory(); // Use the high-level iterators (returns ALL of your objects). try { $objects = $s3->getIterator('ListObjects', array( 'Bucket' => $bucket )); echo "Keys retrieved!\n"; foreach ($objects as $object) { echo $object['Key'] . "\n"; } } catch (S3Exception $e) { echo $e->getMessage() . "\n"; } // Use the plain API (returns ONLY up to 1000 of your objects). try { $result = $s3->listObjects(array('Bucket' => $bucket)); echo "Keys retrieved!\n"; foreach ($result['Contents'] as $object) { echo $object['Key'] . "\n"; } } catch (S3Exception $e) { echo $e->getMessage() . "\n"; }

Related Resources

On this page: