I was not able to find a way to check the metadata fields of an S3 object such as the Content-Type
or the Cache-Control
with the AWS SDK for Java 2.x.
With the AWS SDK for Java 1.x it was as easy as this:
s3Client.getObjectMetadata("myBucket", "myfile.doc");
But I cannot see the analogous method for the newest version of the API.
There are two kinds of metadata in Amazon S3: system-defined metadata and user-defined metadata. The sections below provide more information about system-defined and user-defined metadata. For more information about editing metadata using the Amazon S3 console, see Editing object metadata in the Amazon S3 console.
Our solution is built with Amazon S3 event notifications, AWS Lambda, AWS Glue Catalog, and Amazon Athena. These services allow you to search thousands of objects in an S3 bucket by filenames, object metadata, and object keys.
Some metadata is set by Amazon S3 when you upload the object. For example, Content-Length is the key (name) and the value is the size of the object in bytes. You can also set some metadata when you upload the object and later edit it as your needs change.
The solution is to use HeadObjectRequest
and HeadObjectResponse
:
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
And then:
HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest);
System.out.println("This is what I need: " + headObjectResponse.contentType());
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