How to set the content type of an S3 object via the SDK?

I am trying to use AWS Api to set the content type of several objects and add the header "content-encoding: gzip" to them. Here is my code for this:

for (S3ObjectSummary summary : objs.getObjectSummaries() ) { String key = summary.getKey(); if (! key.endsWith(".gz")) continue; ObjectMetadata metadata = new ObjectMetadata(); metadata.addUserMetadata("Content-Encoding", "gzip"); metadata.addUserMetadata("Content-Type", "application/x-gzip"); final CopyObjectRequest request = new CopyObjectRequest(bucket, key, bucket, key) .withSourceBucketName( bucket ) .withSourceKey(key) .withNewObjectMetadata(metadata); s3.copyObject(request); } 

When I run this, this is the result:

screenshot

As you can see, the prefix x-amz-meta was added to my custom headers, and they were below. And the content-type header was ignored, instead it put www/form-encoded as the header.

What can I do to make it accept the values ​​of my header?

+8
java amazon-s3 amazon-web-services
source share
1 answer

Found a problem. ObjectMetadata requires the content type / encoding to be set explicitly, and not via addUserMetadata() . Change to the following:

  metadata.addUserMetadata("Content-Encoding", "gzip"); metadata.addUserMetadata("Content-Type", "application/x-gzip"); 

to:

  metadata.setContentEncoding("gzip"); metadata.setContentType("application/x-gzip"); 

fixed it.

+11
source share

All Articles