Created
September 19, 2022 15:29
-
-
Save jelmervdl/780dd3a84ce18c07aa377fa48d2ee876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import boto3 | |
import gzip | |
import sys | |
import mimetypes | |
from pprint import pprint | |
from typing import Dict, Any | |
mimetypes.add_type('application/wasm', '.wasm') | |
mimetypes.add_type('text/markdown', '.md') | |
s3 = boto3.resource('s3') | |
bucket = s3.Bucket(sys.argv[1]) | |
for obj_sum in bucket.objects.filter(Prefix=sys.argv[2]): | |
# Skip folders | |
if obj_sum.size == 0: | |
continue | |
obj = obj_sum.Object() | |
update: Dict[str,Any] = dict() | |
print(obj.key) | |
data = obj.get() | |
if obj.content_encoding != 'gzip': | |
print(f"Uncompressed {data['ContentLength']}") | |
compressed = gzip.compress(data['Body'].read()) | |
else: | |
compressed = data['Body'].read() | |
print(f"Compressed {len(compressed)}") | |
update['Body'] = compressed | |
update['ContentEncoding'] = 'gzip' | |
content_type, _ = mimetypes.guess_type(obj.key) | |
print(f"Content-Type: {content_type}") | |
update['ContentType'] = content_type or 'binary/octet-stream' | |
if update and 'Body' in update: | |
obj.put(**update) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment