Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting content-encoding and content-type with Amazon API for .NET

How can I set the S3 bucket file header to be the following:

content-encoding: gzip content-type: text/css

I am using Amazon API using the SDK for .NET. I am uploading a file to S3 using the PutObjectRequest. The problem that when the file is uploaded, the content type and content encoding headers aren't beign updated (I've checked via the files properties on 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 I'm doing wrong?

like image 207
Idan Shechter Avatar asked Oct 06 '11 19:10

Idan Shechter


People also ask

What is content encoding in API?

Content encoding allows API clients to request content to be compressed before being sent back in the response to an API request. This reduces the amount of data that is sent from API Gateway to API clients and decreases the time it takes to transfer the data. You can enable content encoding in the API definition.

What type of API is Amazon?

Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket APIs at any scale. API developers can create APIs that access AWS or other web services, as well as data stored in the AWS Cloud .


2 Answers

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

PutObjectRequest request = new PutObjectRequest
{
    BucketName = bucketName,
    ContentType = "text/css",
    Key = filename
};
request.Headers["Content-Encoding"] = "gzip";
like image 104
dibs487 Avatar answered Sep 27 '22 18:09

dibs487


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";
like image 36
nsimeonov Avatar answered Sep 27 '22 18:09

nsimeonov