Last active
January 15, 2021 11:16
-
-
Save latenssi/70d6706958e98ba4355af16bc8019c0a to your computer and use it in GitHub Desktop.
Add a CacheControl setting to S3 compatible endpoints
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
from boto3 import session | |
ACCESS_ID = "XXX" | |
SECRET_KEY = "XXX" | |
REGION = "XXX" | |
BUCKET = "XXX" | |
session = session.Session() | |
client = session.client('s3', | |
region_name=REGION, | |
endpoint_url=f"https://{REGION}.digitaloceanspaces.com", | |
aws_access_key_id=ACCESS_ID, | |
aws_secret_access_key=SECRET_KEY) | |
items = client.list_objects_v2(Bucket=BUCKET)["Contents"] | |
for i,item in enumerate(items): | |
key = item["Key"] | |
head = client.head_object(Bucket=BUCKET, Key=key) | |
client.copy_object( | |
ACL="public-read", | |
Bucket=BUCKET, | |
Key=key, | |
CopySource={ "Bucket": BUCKET, "Key": key }, | |
CacheControl="public, max-age=31536000, immutable", | |
ContentType=head["ContentType"], | |
MetadataDirective="REPLACE", | |
Metadata = head["Metadata"] | |
) | |
print(f"\rProgress: {i+1} / {len(items)}", end="") | |
print("\nDone") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment