Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 : Listing Object with Metadata in single request

I am new to Amazon services, I am using amazon s3 for file storing. I used following code to listing object.

ListObjectsRequest lor = new ListObjectsRequest().withBucketName("BUCKETNAME");
ObjectListing objectListing = amazonS3Client.listObjects(lor);
for (S3ObjectSummary summary: objectListing.getObjectSummaries()) {
    fileKeys.add(summary.getKey());
}

I want to get the all objects meta data in single above request.

Is this posible..?

like image 948
Shivam Avatar asked Jul 22 '14 10:07

Shivam


1 Answers

There is no API that will give you list of objects along with their metadata.

ListObjectsRequest : This request return a list of summary information about the objects in the specified bucket. Depending on the request parameters, additional information is returned, such as common prefixes if a delimiter was specified. List results are always returned in lexicographic (alphabetical) order.

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 187
Neelam Sharma Avatar answered Nov 03 '22 00:11

Neelam Sharma