Skip to content

Instantly share code, notes, and snippets.

@jamshedazhar
Created June 22, 2018 12:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamshedazhar/c3ee767e6a1b97fe533d068d8d809b82 to your computer and use it in GitHub Desktop.
Save jamshedazhar/c3ee767e6a1b97fe533d068d8d809b82 to your computer and use it in GitHub Desktop.
Sample script to fix ContentType of object stored in AWS S3.
import boto3
s3 = boto3.client('s3')
bucket_name = '<bucket name>'
valid_content_type = ['image/jpeg', 'image/png', 'image/gif']
def is_valid_header(bucket, key):
try:
response = s3.head_object(Bucket=bucket, Key=key)
if 'ContentType' in response and response['ContentType'] in valid_content_type:
return True
else:
return False
except Exception as e:
print(e)
return True
if __name__ == "__main__":
response = s3.list_objects_v2(
Bucket=bucket_name,
MaxKeys=800, # Scan 800 file at a time limit is 1000
Prefix='<file name prifix>'
)
Y = 0
while True:
Y = Y + 800
NextContinuationToken = response['NextContinuationToken']
objects = response['Contents']
for object in objects:
key = object['Key']
if not is_valid_header(bucket_name, key):
resp = s3.copy_object(
ACL='public-read',
Bucket=bucket_name,
ContentType=valid_content_type[1],
CopySource={'Bucket': bucket_name, 'Key': key},
Key=key,
MetadataDirective = 'REPLACE'
)
print(resp)
print("Processed {0} images".format(Y))
response = s3.list_objects_v2(
Bucket=bucket_name,
MaxKeys=800,
Prefix='ng/profile/profile_',
ContinuationToken=NextContinuationToken
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment