Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the MIME types of objects in Amazon S3?

Is there a way to retrieve the MIME types of the objects in S3. I am trying to implement a solution in which I will be getting multiple objects from S3. Instead of using there key and then getting a sub string to calculate the MIME type, can I get the MIME type from Amazon S3 in some way? I am using cloud berry explorer pro and I know it let's you set the MIME type, but how do we retrieve this information using the AWS SDK for .NET or the REST API?

like image 283
Abhi.Net Avatar asked May 24 '12 00:05

Abhi.Net


2 Answers

The REST API offers the HEAD Object operation for this purpose and the AWS SDK for .NET conveniently wraps the very same functionality via the GetObjectMetadata() method:

The GetObjectMetadata operation is used to retrieve information about a specific object or object size, without actually fetching the object itself. This is useful if you're only interested in the object metadata, and don't want to waste bandwidth on the object data. The response is identical to the GetObject response, except that there is no response body. [emphasis mine]

like image 79
Roman Newaza Avatar answered Sep 22 '22 16:09

Roman Newaza


To get the file and mimeType of the file in the same request...

using (var client = AWSClientFactory.CreateAmazonS3Client(region))
{
    var response = client.GetObject(bucket, key);
    var mimeType = response.Headers.ContentType;
    return new StreamWithMimeType(response.ResponseStream, mimeType); 
}
like image 23
Sam Sippe Avatar answered Sep 22 '22 16:09

Sam Sippe