krackoder
krackoder

Reputation: 2981

Iterating and retrieving metadata of all objects in Amazon S3

I am using AWS Java SDK to interact with S3. I want to iterate through all the objects in the storage and retrieve metadata of each object. I can iterate through the objects using lists as:

ObjectListing  list= s3client.listObjects("bucket name");

But I am able to retrieve only summaries through the object in the list. Instead of summary I need metadata of each object, like the one provided by getObjectMetadata() method in S3Object class. How do I get that?

Upvotes: 10

Views: 16747

Answers (1)

Neelam Sharma
Neelam Sharma

Reputation: 2855

You can get four default metadata from objectSummary that returned from lisObject : Last Modified, Storage Type, Etag and Size.

To get metadata of objects, you need to perform HEAD object request on object or you call following method on your object :

GetObjectMetadataRequest(String bucketName, String key)

Look at this:

 ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                    .withBucketName(bucketName);
            ObjectListing objectListing;
            do {
                objectListing = s3client.listObjects(listObjectsRequest);
                for (S3ObjectSummary objectSummary
                        : objectListing.getObjectSummaries()) {
                    /** Default Metadata **/
                    Date dtLastModified = objectSummary.getLastModified();
                    String sEtag = objectSummary.getETag();
                    long lSize = objectSummary.getSize();
                    String sStorageClass = objectSummary.getStorageClass();
                    /** To get user defined metadata **/
                    ObjectMetadata objectMetadata = s3client.getObjectMetadata(bucketName, objectSummary.getKey());
                    Map userMetadataMap = objectMetadata.getUserMetadata();
                    Map rowMetadataMap = objectMetadata.getRawMetadata();
                }
                listObjectsRequest.setMarker(objectListing.getNextMarker());
            } while (objectListing.isTruncated());

For more details on GetObjectMetadataRequest, look this link.

Upvotes: 11

Related Questions