C# Find MIME Types for S3 Using An Extension Exception List

Updated on 11th October 2020

I recently had to build a system for checking the MIME type of a file before it gets uploaded to AWS S3. An example of this can be found in my last blog post.

You see, when registering a pre-signed key with the S3 API via your backend, you need to provide the MIME type of the expected file that will be uploaded from the frontend.

If your MIME type is missing or incorrect then the S3 API returns an error message when you try to upload the file.

I'm using an ASP.NET Core 3 backend to generate the pre-signed keys. And the file is never uploaded to the backend, and therefore we have two choices

  1. Get the MIME type from the frontend using Javascript (This is the better solution). Check out this Stack Overflow post for more information.
  2. Alternatively, we can guess the MIME type of the file from the backend.

I had to choose option 2, because only a limited number of file types will be accepted and a device will connect to the API which cannot use solution 1.

Note that this solution will also work for other use-cases, but i'm using it for the purpose of generating pre-signed keys for the S3 API.

The Solution

We're going to write a method that accepts a string as an input parameter. Our method will then return a string containing the guessed MIME type.

The Code

The code is fairly straightforward. We start by getting a list of accepted Mime Types before trying to match the inputted file extension with one in the list. If there's a match, then the appropriate MIME type is returned. Otherwise a null response will be returned.

        public static string CalculateMimeType(string extension)
        {
            var mimetypes = GetAcceptedMimetypes();

            var matchedMimeType = mimetypes
                .Where(m => m.Extension.ToLower() == extension.ToLower())
                .FirstOrDefault();

            if (matchedMimeType != null)
            {
                return matchedMimeType.Mimetype;
            }
            else
            {
                return null;
            }
        }

        public static List<Models.AcceptedMimetypes> GetAcceptedMimetypes()
        {
            List<Models.AcceptedMimetypes> mimetypes = new List<Models.AcceptedMimetypes>();

            mimetypes.Add(new Models.AcceptedMimetypes
            {
                Extension = ".mp4",
                Mimetype = "video/mp4"
            });

            mimetypes.Add(new Models.AcceptedMimetypes
            {
                Extension = ".webm",
                Mimetype = "video/webm"
            });

            mimetypes.Add(new Models.AcceptedMimetypes
            {
                Extension = ".mkv",
                Mimetype = "video/x-matroska"
            });

            return mimetypes;
        }

You might want to replace the GetAcceptedMimeTypes method with something that retrieves the values from a config file. Otherwise it should fit most requirements.


Article Categories: # c# # .net # aws

About

A tech blog by Andy P. I talk about coding, enterprise software development, tech, games design & other things that interest me.

Signup To The Newsletter

I try to post a new interesting article every saturday.