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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With