Home > Back-end >  Python Get MIME of s3 object on Lambda
Python Get MIME of s3 object on Lambda

Time:01-03

I have a lambda that triggers upon s3 PutObject. Before proceeding the lambda needs to check if the file is actually a video file or not (mp4 in my case). File extension is not helpful because that can be fake. So I have tried checking mime type

CodePudding user response:

Boto3 has a function S3.Client.head_object:

The HEAD action retrieves metadata from an object without returning the object itself. This action is useful if you're only interested in an object's metadata. To use HEAD, you must have READ access to the object.

You can call this method to get metadata object associated with S3 bucket item.

metadata = s3client.head_object(Bucket='MyBucketName', Key='MyS3ItemKey')

This metadata includes a ContentType property, you can use this property to check the object type.

OR

If you can't trust this ContentType as this can be faked. You can simply save the object's MIME type in DynamoDB while uploading it. You can read the type from there whenever you want.

OR

You can simply create a Lambda that will get triggered, you can download the object in the Lambda as it has around 512MB as ephemeral storage. You can determine the content type there and update it, as you can also set some metadata when you upload the object and later edit it as your needs change.

  • Related