Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 518
krackoder Avatar asked Jul 24 '14 16:07

krackoder


1 Answers

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.

like image 68
Neelam Sharma Avatar answered Oct 22 '22 04:10

Neelam Sharma