Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Listing Keys Using the AWS SDK for Java

The following tasks guide you through using the Java classes to list object keys in a bucket. You have many options for listing the objects in your bucket. Note that for buckets with large number of objects you might get truncated results when listing the objects. You should check to see if the returned object listing is truncated, and use the AmazonS3.listNextBatchOfObjects operation to retrieve additional results.

Listing Object Keys

1

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

2

Execute one of the AmazonS3Client.listObjects methods. You need to provide the request information, such as the bucket name, and the optional key prefix. You provide this information by creating an instance of the ListObjectsRequest class. For listing objects, Amazon S3 returns up to 1,000 keys in the response. If you have more than 1,000 keys in your bucket, the response will be truncated. You should always check for if the response is truncated.


The following Java code sample demonstrates the preceding tasks.

AWSCredentials myCredentials = new BasicAWSCredentials(
                                 myAccessKeyID, mySecretKey);
AmazonS3 s3client = new AmazonS3Client(myCredentials);        

ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("m");
ObjectListing objectListing;

do {
	objectListing = s3client.listObjects(listObjectsRequest);
	for (S3ObjectSummary objectSummary : 
		objectListing.getObjectSummaries()) {
		System.out.println( " - " + objectSummary.getKey() + "  " +
                "(size = " + objectSummary.getSize() + 
				")");
	}
	listObjectsRequest.setMarker(objectListing.getNextMarker());
} while (objectListing.isTruncated());

Example

The following Java code example list object keys in an Amazon S3 bucket. For instructions on how to create and test a working sample, see Testing the Java Code Examples

import java.io.IOException;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class S3Sample {
	private static String bucketName = "*** Provide bucket name ***";
	
	public static void main(String[] args) throws IOException {
        AmazonS3 s3client = new AmazonS3Client(new PropertiesCredentials(
                S3Sample.class.getResourceAsStream(
                		"AwsCredentials.properties")));
        try {
            System.out.println("Listing objects");
   
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(bucketName)
                .withPrefix("m");
            ObjectListing objectListing;            
            do {
                objectListing = s3client.listObjects(listObjectsRequest);
                for (S3ObjectSummary objectSummary : 
                	objectListing.getObjectSummaries()) {
                    System.out.println(" - " + objectSummary.getKey() + "  " +
                            "(size = " + objectSummary.getSize() + 
                            ")");
                }
                listObjectsRequest.setMarker(objectListing.getNextMarker());
            } while (objectListing.isTruncated());
         } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, " +
            		"which means your request made it " +
                    "to Amazon S3, but was rejected with an error response " +
                    "for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, " +
            		"which means the client encountered " +
                    "an internal error while trying to communicate" +
                    " with S3, " +
                    "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }
}