Browse Source

Merge pull request #125 from jrstarke/s3_cache_control

Added Cache Control metadata to S3 upload
pull/136/head
Simon Conseil 12 years ago
parent
commit
797e601775
  1. 32
      sigal/plugins/upload_s3.py

32
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):

Loading…
Cancel
Save