Skip to content

Instantly share code, notes, and snippets.

@evz
Created October 7, 2012 21:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save evz/3849704 to your computer and use it in GitHub Desktop.
Save evz/3849704 to your computer and use it in GitHub Desktop.
S3 Publisher for Hyde (static site generator written in Python)

How to use

  • Add the s3.py file to your project. As an example, put it inside a folder called publishers.
  • The folder where s3.py resides must be a Python package, so add an empty __init__.py file to that folder also.
  • Add the following configuration to your site.yaml and modify it as needed:
publisher:
    s3:
        type: publishers.s3.S3  # here, publishers is the folder where the s3.py file was placed
            key: my_access_key_id 
            secret: my_secret_access_key
            bucket: www.my-bucket-name.com
            redirects:
                - /source1.html => /destination1.html
                - /source2.html => /destination2.html

The key and secret settings can be omitted and instead set as the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Using environment variables might be preferable as the configuration file can then be commited and made public without revealing the AWS credentials.

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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment