From 81e8f6d6b1250435a94302e5e671af70bc3bcd54 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 20 Jan 2017 22:27:46 -0800 Subject: [PATCH] adding an s3 publisher.. --- publishers/__init__.py | 0 publishers/s3.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 publishers/__init__.py create mode 100644 publishers/s3.py diff --git a/publishers/__init__.py b/publishers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/publishers/s3.py b/publishers/s3.py new file mode 100644 index 0000000..443dd72 --- /dev/null +++ b/publishers/s3.py @@ -0,0 +1,51 @@ +#from: https://gist.github.com/evz/3849704 +from hyde.publisher import Publisher +from boto.s3.connection import S3Connection +from boto.s3.key import Key +import os + + +class S3CredentialsError(Exception): + def __init__(self, value): + self.value = value + + def __str__(self): + return repr(self.value) + + +class S3(Publisher): + def initialize(self, settings): + self.settings = settings + self.key = getattr(settings, 'key', None) + self.secret = getattr(settings, 'secret', None) + self.bucket = getattr(settings, 'bucket', None) + reds = getattr(settings, 'redirects', None) + self.redirects = [] + if reds: + for redirect in reds: + f, t = redirect.split(' => ') + self.redirects.append({'from': f, 'to': t}) + if not self.key or not self.secret: + try: + self.key = os.environ['AWS_ACCESS_KEY_ID'] + self.secret = os.environ['AWS_SECRET_ACCESS_KEY'] + except KeyError: + raise S3CredentialsError('You need to define both an AWS key and secret in your site.yaml or as environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY') + + def publish(self): + super(S3, self).publish() + conn = S3Connection(self.key, self.secret) + bucket = conn.get_bucket(self.bucket) + k = Key(bucket) + root = self.site.config.deploy_root_path + for item in root.walker.walk_files(): + redirect_meta = None + if self.redirects: + for redirect in self.redirects: + if redirect['from'] in item.path: + redirect_meta = redirect['to'] + k.key = item.path.replace(str(root), '') + k.set_contents_from_filename(item.path) + if redirect_meta: + k.set_metadata('website-redirect-location', redirect_meta) + k.set_acl('public-read')