Skip to content

determine real file type (MIME) of an uploaded object in s3

0

Hello Team,

Is there a time-efficient way to determine the MIME type of an object uploaded to an S3 bucket without downloading the entire object and using fileTypeFromFile from the 'file-type' library?

Relying on the headObject API is not reliable, as S3 determines the MIME type based on the file extension, not the actual content type. For example, if a file named myfile.zip is uploaded as myfile.jpeg, the headObject response will return jpeg as the MIME type, rather than the correct type application/zip.

Note, I cannot set the Content-Type during the upload process, as I do not control the upload itself, the file is uploaded from the client browser using signed URLs — I only provide pre-signed URLs and CloudFront signed URLs for file uploads.

downloading the file in my lambda local storage /tmp to determine the MIME type via fileTypeFromFile function from the 'file-type' library is time consuming.

Any insights on this would be greatly appreciated.

2 Answers
0

There are a few approaches you can consider to determine the real MIME type of an object uploaded to S3 without downloading the entire file:

  1. Use AWS Lambda with Python-Magic library: You can create a Lambda function triggered by S3 upload events. This function can use the python-magic library to analyze the first few bytes of the file to determine its true MIME type. This method is more reliable than relying on file extensions and doesn't require downloading the entire file.

  2. Implement a custom solution using AWS Lambda: Create a Lambda function that reads a small portion of the file (e.g., the first few kilobytes) using the S3 GetObject API with a Range header. Then, use this data to determine the file type based on its signature or magic numbers.

  3. Use S3 Select: For certain file types (CSV, JSON, Parquet), you can use S3 Select to query the first few bytes of the object without retrieving the entire file. This can help in determining the file type for these specific formats.

  4. Implement client-side MIME type detection: If possible, you could implement MIME type detection on the client-side before the upload occurs. This would allow you to send the correct Content-Type when generating the pre-signed URL.

  5. Use AWS Glue: For large-scale processing, you could consider using AWS Glue to catalog your S3 objects and determine their types as part of an ETL process.

Each of these methods has its trade-offs in terms of accuracy, performance, and implementation complexity. The Lambda function with Python-Magic or a custom implementation reading the first few bytes of the file are likely to be the most versatile and accurate solutions for your use case.
Sources
Process Objects on Amazon S3 with Precision File Type Detection | AWS re:Post
Use HeadObject with an AWS SDK or CLI - Amazon Simple Storage Service
Tutorial: Using an Amazon S3 trigger to invoke a Lambda function - AWS Lambda

answered a year ago

0

You can implement a Lambda function that uses S3's Range header capability. By requesting only the first few kilobytes of an object using s3.getObject() with the Range: 'bytes=0-4095' parameter, you can examine file signatures (magic numbers) to accurately identify the file type regardless of extension.

Documentation References:

AWS

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.