Skip to content

Instantly share code, notes, and snippets.

@CHBresser
Last active August 1, 2022 13:34
Show Gist options
  • Save CHBresser/c5e05a676fcb87132676a504005e0f6a to your computer and use it in GitHub Desktop.
Save CHBresser/c5e05a676fcb87132676a504005e0f6a to your computer and use it in GitHub Desktop.
Compress S3 Images using Boto3
import sys, botocore
import boto3
from io import BytesIO
from PIL import Image as pil
def start_compressing(dry_run=False):
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucketname')
exists = True
try:
s3.meta.client.head_bucket(Bucket='mybucketname')
except botocore.exceptions.ClientError as e:
# If a client error is thrown, then check that it was a 404 error.
# If it was a 404 error, then the bucket does not exist.
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
i = 0
for file in bucket.objects.all():
if file.key[-4:] == '.jpg' or file.key[-4:] == '.png':
if file.size > 150000:
if not dry_run:
compress_image(file, i)
i += 1
if dry_run:
print('Will compress {0} images'.format(i))
else:
print('Compressed {0} images'.format(i))
def compress_image(pic, index):
print('{0}: compressing image: {1}'.format(index, pic.key))
input_file = BytesIO(pic.get()['Body'].read())
img = pil.open(input_file)
tmp = BytesIO()
img.save(tmp, 'JPEG', quality=50)
tmp.seek(0)
output_data = tmp.getvalue()
content_type = 'image/jpeg'
content_length = len(output_data)
cache_control = 'max-age = 604800'
pic.put(Body=output_data, CacheControl=cache_control, ContentLength=content_length, ContentType=content_type, ServerSideEncryption='AES256', ACL='public-read')
tmp.close()
input_file.close()
if __name__ == "__main__":
if len(sys.argv) > 1:
command = sys.argv[1]
if command == 'dry_run':
start_compressing(dry_run=True)
else:
print('Invalid command. For a dry run please run: python compress_s3_images.py dry_run')
else:
start_compressing()
print("end")
sys.exit()
@CHBresser
Copy link
Author

CHBresser commented Jun 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment