Skip to content

Instantly share code, notes, and snippets.

@latenssi
Last active January 15, 2021 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latenssi/70d6706958e98ba4355af16bc8019c0a to your computer and use it in GitHub Desktop.
Save latenssi/70d6706958e98ba4355af16bc8019c0a to your computer and use it in GitHub Desktop.
Add a CacheControl setting to S3 compatible endpoints
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