Skip to content

Instantly share code, notes, and snippets.

@thomasyip
Created July 20, 2012 23:41
Show Gist options
  • Save thomasyip/3153886 to your computer and use it in GitHub Desktop.
Save thomasyip/3153886 to your computer and use it in GitHub Desktop.
class S3BotoStaticStorage (to deploy of django `media` and `static` into two amazon bucket)
from __future__ import absolute_import
import settings
from storages.backends.s3boto import S3BotoStorage
from boto.s3.connection import SubdomainCallingFormat
def getattr_backup(obj, key, backupkey, default):
result = getattr(obj, key, None)
if result is None:
result = getattr(obj, backupkey, default)
return result
ACCESS_KEY_NAME = getattr_backup(settings, 'AWS_STATIC_ACCESS_KEY_ID', 'AWS_ACCESS_KEY_ID', None)
SECRET_KEY_NAME = getattr_backup(settings, 'AWS_STATIC_SECRET_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY', None)
HEADERS = getattr_backup(settings, 'AWS_STATIC_HEADERS', 'AWS_HEADERS', {})
STORAGE_BUCKET_NAME = getattr_backup(settings, 'AWS_STATIC_STORAGE_BUCKET_NAME', 'AWS_STORAGE_BUCKET_NAME', None)
AUTO_CREATE_BUCKET = getattr_backup(settings, 'AWS_STATIC_AUTO_CREATE_BUCKET', 'AWS_AUTO_CREATE_BUCKET', False)
DEFAULT_ACL = getattr_backup(settings, 'AWS_STATIC_DEFAULT_ACL', 'AWS_DEFAULT_ACL', 'public-read')
BUCKET_ACL = getattr_backup(settings, 'AWS_STATIC_BUCKET_ACL', 'AWS_BUCKET_ACL', DEFAULT_ACL)
QUERYSTRING_AUTH = getattr_backup(settings, 'AWS_STATIC_QUERYSTRING_AUTH', 'AWS_QUERYSTRING_AUTH', True)
QUERYSTRING_EXPIRE = getattr_backup(settings, 'AWS_STATIC_QUERYSTRING_EXPIRE', 'AWS_QUERYSTRING_EXPIRE', 3600)
REDUCED_REDUNDANCY = getattr_backup(settings, 'AWS_STATIC_REDUCED_REDUNDANCY', 'AWS_REDUCED_REDUNDANCY', False)
LOCATION = getattr_backup(settings, 'AWS_STATIC_LOCATION', 'AWS_LOCATION', '')
CUSTOM_DOMAIN = getattr_backup(settings, 'AWS_STATIC_S3_CUSTOM_DOMAIN', 'AWS_S3_CUSTOM_DOMAIN', None)
CALLING_FORMAT = getattr_backup(settings, 'AWS_STATIC_S3_CALLING_FORMAT', 'AWS_S3_CALLING_FORMAT', SubdomainCallingFormat())
SECURE_URLS = getattr_backup(settings, 'AWS_STATIC_S3_SECURE_URLS', 'AWS_S3_SECURE_URLS', True)
FILE_NAME_CHARSET = getattr_backup(settings, 'AWS_STATIC_S3_FILE_NAME_CHARSET', 'AWS_S3_FILE_NAME_CHARSET', 'utf-8')
FILE_OVERWRITE = getattr_backup(settings, 'AWS_STATIC_S3_FILE_OVERWRITE', 'AWS_S3_FILE_OVERWRITE', True)
IS_GZIPPED = getattr_backup(settings, 'AWS_STATIC_IS_GZIPPED', 'AWS_IS_GZIPPED', False)
PRELOAD_METADATA = getattr_backup(settings, 'AWS_STATIC_PRELOAD_METADATA', 'AWS_PRELOAD_METADATA', False)
GZIP_CONTENT_TYPES = getattr_backup(settings, 'STATIC_GZIP_CONTENT_TYPES', 'GZIP_CONTENT_TYPES', (
'text/css',
'application/javascript',
'application/x-javascript'
))
class S3BotoStaticStorage(S3BotoStorage):
def __init__(self, bucket=STORAGE_BUCKET_NAME, access_key=None,
secret_key=None, bucket_acl=BUCKET_ACL, acl=DEFAULT_ACL, headers=HEADERS,
gzip=IS_GZIPPED, gzip_content_types=GZIP_CONTENT_TYPES,
querystring_auth=QUERYSTRING_AUTH, querystring_expire=QUERYSTRING_EXPIRE,
reduced_redundancy=REDUCED_REDUNDANCY,
custom_domain=CUSTOM_DOMAIN, secure_urls=SECURE_URLS,
location=LOCATION, file_name_charset=FILE_NAME_CHARSET,
preload_metadata=PRELOAD_METADATA, calling_format=CALLING_FORMAT):
return super(S3BotoStaticStorage, self).__init__(
bucket=bucket, access_key=None,
secret_key=None, bucket_acl=bucket_acl, acl=acl, headers=headers,
gzip=gzip, gzip_content_types=gzip_content_types,
querystring_auth=querystring_auth, querystring_expire=querystring_expire,
reduced_redundancy=reduced_redundancy,
custom_domain=custom_domain, secure_urls=secure_urls,
location=location, file_name_charset=file_name_charset,
preload_metadata=preload_metadata, calling_format=calling_format
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment