import email import mimetypes import os import re import time from datetime import datetime, timedelta from django.utils.text import compress_string from your_project.settings import MEDIA_ROOT AWS_ACCESS_KEY_ID = 'YOUR_AWS_ACCESS_KEY' AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET_KEY' BUCKET_NAME = 'YOUR_AWS_BUCKET_NAME' YUI_COMPRESSOR = 'java -jar /path/to/yuicompressor.jar %s -o %s' MEDIA_TYPES = ('text/css', 'application/javascript', 'application/x-javascript',) try: from boto.s3.connection import S3Connection from boto.s3.key import Key except ImportError: raise ImportError('Please install Boto => http://code.google.com/p/boto/') def update_s3(): this_path = os.path.abspath('.') this_folder = re.sub(MEDIA_ROOT, '', this_path) for item in os.listdir('.'): if item.lower() in ('.svn', '.ds_store',): continue filename = os.path.normpath(item) content_type = mimetypes.guess_type(filename)[0] if os.path.isfile(filename): if content_type in MEDIA_TYPES: content = yui_file(this_path, filename) else: content = open('%s/%s' % (this_path, filename,), 'rb').read() headers = {} headers['Content-Type'] = content_type if content_type else 'text/plain' expires = (datetime.now() + timedelta(days=365*2)).timetuple() headers['Expires'] = '%s GMT' % email.Utils.formatdate(time.mktime(expires)) if headers['Content-Type'] in MEDIA_TYPES: headers['Content-Encoding'] = 'gzip' content = compress_string(content) s3_filename = '%s/%s' % (this_folder, filename,) if len(this_folder) > 1 else filename store_in_s3(s3_filename, content, headers) def store_in_s3(filename, content, headers): conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) b = conn.create_bucket(BUCKET_NAME) k = Key(b) k.key = filename k.set_contents_from_string(content, headers, replace=True) k.set_acl('public-read') def yui_file(this_path, filename): old_file = '%s/%s' % (this_path, filename,) new_file = '/tmp/%s' % filename status = os.system(YUI_COMPRESSOR % (old_file, new_file,)) if status != 0: raise BuildError('Woops! Something went wrong with the YUI Compressor!') compressed = open(new_file, 'rb').read() os.remove(new_file) return compressed if __name__ == "__main__": update_s3()