From 3ec6667e8c09ba754e354a942216add3eff88f86 Mon Sep 17 00:00:00 2001 From: Jamie Starke Date: Sun, 26 Oct 2014 19:53:33 -0700 Subject: [PATCH] Added Cache Control metadata to S3 upload Added two new properties for the S3 Upload plugin, allowing to set the default S3 cache control header, and to set for media types (jpg, png, webm). These properties are then used to generate the max-age property when uploading to S3, or when checking against existing files --- sigal/plugins/upload_s3.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/sigal/plugins/upload_s3.py b/sigal/plugins/upload_s3.py index 71404be..3025f0c 100644 --- a/sigal/plugins/upload_s3.py +++ b/sigal/plugins/upload_s3.py @@ -3,7 +3,7 @@ """Plugin to upload generated files to Amazon S3. This plugin requires boto_. All generated files are uploaded to a specified S3 bucket. -When using this plugin you have to make sure that the bucket already exists and the +When using this plugin you have to make sure that the bucket already exists and the you have access to the S3 bucket. The access credentials are managed by boto_ and can be given as environment variables, configuration files etc. More information can be found on the boto_ documentation. @@ -17,6 +17,10 @@ Settings (all settings are wrapped in ``upload_s3_options`` dict): private, public-read, public-read-write, authenticated-read - ``overwrite``: Boolean indicating if all files should be uploaded and overwritten or if already uploaded files should be skipped. +- ``max_age``: Optional, Integer indicating the number of seconds that the cache + control should be set by default +- ``media_max_age``: Optional, Integer indicates the number of seconds that + cache control hould be set for media files """ @@ -51,6 +55,11 @@ def upload_s3(gallery, settings=None): # Check if file was uploaded before key = bucket.get_key(f) if key != None and key.size == size: + cache_metadata = generate_cache_metadata(gallery,f) + + if key.get_metadata('Cache-Control') != cache_metadata: + key.set_remote_metadata({ + 'Cache-Control':cache_metadata},{},True); logger.debug("Skipping file %s" % (f)) else: upload_file(gallery, bucket, f) @@ -58,12 +67,31 @@ def upload_s3(gallery, settings=None): # File is not available on S3 yet upload_file(gallery, bucket, f) +def generate_cache_metadata(gallery, f): + filename, file_extension = os.path.splitext(f) + + proposed_cache_control = None + if 'media_max_age' in gallery.settings['upload_s3_options'] and \ + file_extension in ['.jpg','.png','.webm']: + proposed_cache_control = "max-age=%s" % \ + gallery.settings['upload_s3_options']['media_max_age'] + elif 'max_age' in gallery.settings['upload_s3_options']: + proposed_cache_control = "max-age=%s" % \ + gallery.settings['upload_s3_options']['max_age'] + return proposed_cache_control + def upload_file(gallery, bucket, f): logger.debug("Uploading file %s" % (f)) + key = Key(bucket) key.key = f + + cache_metadata = generate_cache_metadata(gallery, f) + if cache_metadata: + key.set_metadata('Cache-Control', cache_metadata) + key.set_contents_from_filename( - os.path.join(gallery.settings['destination'], f), + os.path.join(gallery.settings['destination'], f), policy = gallery.settings['upload_s3_options']['policy']) def register(settings):