Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check metadata of S3 objects with AWS SDK for Java 2.x

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.

like image 864
Arcones Avatar asked Jul 09 '19 09:07

Arcones


People also ask

Does S3 have metadata?

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.

Can Athena query metadata S3?

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.

What is metadata in AWS S3?

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.


1 Answers

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());
like image 160
Arcones Avatar answered Oct 04 '22 14:10

Arcones