Skip to content

Commit

Permalink
fix(cf-reset-cache): handle s3 bucket names with periods in them
Browse files Browse the repository at this point in the history
It looks like boto in python 2 does not handle bucket names with periods
in them [1]. An SSl CertificateError is thrown.

Looks like possible solutions are monkey patching, adding a check for
period to determine which S3 Calling Method to use, or update to boto3.

Opted to update to boto3.

[1] boto/boto#2836
  • Loading branch information
dustinspecker committed Sep 3, 2019
1 parent 03c295b commit 7160680
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cf-reset-cache/Dockerfile
Expand Up @@ -3,7 +3,8 @@ LABEL maintainer "Jessie Frazelle <jess@linux.com>"

RUN apk add --no-cache \
ca-certificates \
py-boto
py-pip \
&& pip install boto3


COPY ./reset-cache.py /bin/reset-cache.py
Expand Down
34 changes: 22 additions & 12 deletions cf-reset-cache/reset-cache.py
@@ -1,8 +1,9 @@
#!/usr/local/bin/python

import boto
import boto3
import os
import sys
import uuid


access_key = os.getenv("AWS_ACCESS_KEY")
Expand All @@ -24,20 +25,29 @@
sys.exit(1)

# get the paths from s3
s3_conn = boto.connect_s3(access_key, access_secret)
docs = s3_conn.get_bucket(bucket)
s3_conn = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=access_secret)
docs = s3_conn.list_objects(Bucket=bucket)
items = []

for key in docs.list():
index_file = "/index.html"
if key.name.endswith((index_file)):
for key in docs['Contents']:
name = key['Key'].encode('utf-8')
index_file = "index.html"
if name.endswith((index_file)):
# append the file without the postfix as well
items.append(key.name.replace(index_file, ""))
items.append(key.name.replace(index_file, "/"))
items.append(key.name)

cf_conn = boto.connect_cloudfront(access_key, access_secret)
inval_req = cf_conn.create_invalidation_request(cloudfront_dist, items)
items.append(name.replace(index_file, "/"))
items.append('/{}'.format(name))

cf_conn = boto3.client('cloudfront', aws_access_key_id=access_key, aws_secret_access_key=access_secret)
inval_req = cf_conn.create_invalidation(
DistributionId=cloudfront_dist,
InvalidationBatch={
'Paths': {
'Quantity': len(items),
'Items': items,
},
'CallerReference': str(uuid.uuid4())
}
)

print "Invalidating these files: "
print items
Expand Down

0 comments on commit 7160680

Please sign in to comment.