Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 843
Ali Avatar asked May 05 '14 07:05

Ali


1 Answers

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.

like image 66
Ali Avatar answered Oct 08 '22 15:10

Ali