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

爱⌒轻易说出口 提交于 2020-04-11 12:34:21

问题


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.


回答1:


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());


来源:https://stackoverflow.com/questions/56949135/check-metadata-of-s3-objects-with-aws-sdk-for-java-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!