Question

I'm trying to use AWS Api to set the content type of multiple objects and to add a 'content-encoding: gzip' header to them. Here's my code for doing that:

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 however, the following is the result:

screenshot

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

What can I do it to cause it to accept my header values?

Was it helpful?

Solution

Found the problem. ObjectMetadata requires the content-type / encoding to be set explicitly rather than via addUserMetadata(). Changing the following:

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

to:

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

fixed this.

OTHER TIPS

In SDK V2, use:

PutObjectRequest.builder() 
...
.contentType(contentType)
.build()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top