Set content encoding and content type using the Amazon API for .NET.

How to configure the S3 byte file header as follows:

content encoding: gzip content-type: text / css

I am using the Amazon API using the SDK for .NET. I upload a file to S3 using PutObjectRequest. The problem is that when loading a file, the content headers and the content headers are not updated (I checked the file properties on the Amazon console).

Example:

request.AddHeader("expires", EXPIRES_DATE); request.AddHeader("content-encoding", "gzip"); request.ContentType = "text/css"; 

Also tried:

 NameValueCollection metadata = new NameValueCollection(); metadata.Add("content-type", "text/css"); metadata.Add("content-encoding", "gzip"); request.WithMetaData(metadata); 

What am I doing wrong?

+4
source share
4 answers

you add the content type to the contentType property in the api as shown below.

  PutObjectRequest request = new PutObjectRequest(); string contentType = "text/css"; request = request.WithContentType(contentType); 

// to encode content // add the following header

 request.AddHeader("content-encoding","gzip"); 

hope this can help

+1
source

The accepted answer was correct at the time, but the API was updated in 2014, so this is a new way

 PutObjectRequest request = new PutObjectRequest { BucketName = bucketName, ContentType = "text/css", Key = filename }; request.Headers["Content-Encoding"] = "gzip"; 
+2
source

In 2019, using the latest API, the code will look like this:

 PutObjectRequest request = new PutObjectRequest { BucketName = bucketName, ContentType = "text/css", Key = filename }; request.Headers.ContentEncoding = "gzip"; 
+2
source

It’s great that people add comments to update the response when the API changes. I tried all three, I can confirm that nsimeonov's answer is currently a supported way to do this. Thanks for posting.

0
source

All Articles