-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_cache_control.py
58 lines (45 loc) · 1.85 KB
/
update_cache_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Tool for setting the cache-control header on items in an S3 bucket."""
# pylint: disable=import-error
import boto3
# pylint: enable=import-error
SESSION = boto3.Session(profile_name="codependentcodr")
CLIENT = SESSION.client("s3")
CACHEABLE_TYPES = ["png", "jpeg", "jpg", "css", "js", "gif"]
SECONDS_IN_A_WEEK = 60 * 60 * 24 * 7
CACHE_CONTROL_SETTING = f"public, max-age={SECONDS_IN_A_WEEK}"
def update_file(bucket, key):
"""Sets the cache-control header on the item in the bucket."""
if any(key.endswith(cache_type) for cache_type in CACHEABLE_TYPES):
item = CLIENT.head_object(Bucket=bucket, Key=key)
cache_control = item.get("CacheControl", None)
content_type = item.get("ContentType", None)
if not cache_control:
print(
f"No Cache Control set for {bucket}/{key}, updating to {CACHE_CONTROL_SETTING}"
)
CLIENT.copy_object(
Bucket=bucket,
Key=key,
CopySource=bucket + "/" + key,
CacheControl=CACHE_CONTROL_SETTING,
ContentType=content_type,
Metadata=item["Metadata"],
MetadataDirective="REPLACE",
)
return True
return False
def process_bucket(bucket):
"""For all items in the given bucket, apply update_file."""
print(f"Processing {bucket}...")
paginator = CLIENT.get_paginator("list_objects_v2")
page_iterator = paginator.paginate(Bucket=bucket)
count = 0
for page in page_iterator:
for item in page["Contents"]:
key = item["Key"]
if update_file(bucket, key):
count += 1
print(f"Updated count: {count}")
if __name__ == "__main__":
process_bucket("www.codependentcodr.com")
# update_file('www.codependentcodr.com', 'static/imgs/cloudfront-behaviour-1.png')