Access to S3 uses a library called boto3. The file type is obtained in mime-type format using libmagic.
First, install the libraries:
$ brew install libmagic
$ pip install python-magic
$ pip install boto3
Then the script:
#Target file path prefix
key_prefix = 'uploads/'
#Target file name pattern
key_pattern = 'uploads/\d+/images/.*'
#Temporary file name for download
temporary_filename = '/tmp/downloaded_file'
for s3_object in bucket.objects.filter(Prefix=key_prefix):
if not re.match(key_pattern, s3_object.key):
continue
s3_object.Object().download_file(temporary_filename)
mime_type = magic.from_file(temporary_filename, mime=True)
print("{}: {}".format(s3_object.key, mime_type)