Home > Mobile >  AWS S3 list all files with specific content type
AWS S3 list all files with specific content type

Time:02-28

I want to CLI list all S3 bucket files that have content type = binary/octet-stream.

aws s3 ls s3://mybucket -r -content-type???
  1. How to list files with content type = binary/octet-stream?
  2. How to list all files with the content type of each file?

CodePudding user response:

You would need to:

  • List the objects in the bucket
  • For each object, call aws s3api head-object --bucket xxx --key xxx

It will return:

{
    "AcceptRanges": "bytes",
    "LastModified": "2014-03-10T21:59:20 00:00",
    "ContentLength": 41603,
    "ETag": "\"eca134ebe408fdb1f3494d7d916bf027\"",
    "VersionId": "null",
    "ContentType": "image/jpeg",
    "ServerSideEncryption": "AES256",
    "Metadata": {}
}

You would need some shell-scripting skills to be able to do this with the AWS CLI. It would be easier to accomplish with a scripting language, such as Python:

import boto3

s3_resource = boto3.resource('s3')

for object in s3_resource.Bucket('BUCKETNAME').objects.all():
    print(object.key, object.get()['ContentType'])
  • Related