<s> import boto <EOL> from boto . s3 . key import Key <EOL> from boto . s3 . connection import OrdinaryCallingFormat <EOL> from django . conf import settings <EOL> import urllib <EOL> def _ensure_bucket_exists ( s3 , bucket ) : <EOL> try : <EOL> s3 . create_bucket ( bucket ) <EOL> except boto . exception . S3ResponseError : <EOL> pass <EOL> else : <EOL> print "<STR_LIT>" % bucket <EOL> if settings . AWS_ENABLED : <EOL> if settings . AWS_S3_FAKE_S3 is None : <EOL> _s3 = boto . connect_s3 ( settings . AWS_ACCESS_KEY_ID , settings . AWS_SECRET_ACCESS_KEY ) <EOL> else : <EOL> host , port = ( settings . AWS_S3_FAKE_S3 . split ( '<STR_LIT::>' , <NUM_LIT:2> ) + [ <NUM_LIT> ] ) [ : <NUM_LIT:2> ] <EOL> port = int ( port ) <EOL> _s3 = boto . connect_s3 ( "<STR_LIT>" , "<STR_LIT>" , is_secure = False , port = port , <EOL> host = host , calling_format = OrdinaryCallingFormat ( ) ) <EOL> _ensure_bucket_exists ( _s3 , settings . AWS_S3_SOURCE_BUCKET ) <EOL> _ensure_bucket_exists ( _s3 , settings . AWS_S3_EXPORT_BUCKET ) <EOL> _ensure_bucket_exists ( _s3 , settings . AWS_S3_BUILDS_BUCKET ) <EOL> _buckets = { <EOL> '<STR_LIT:source>' : _s3 . get_bucket ( settings . AWS_S3_SOURCE_BUCKET ) , <EOL> '<STR_LIT>' : _s3 . get_bucket ( settings . AWS_S3_EXPORT_BUCKET ) , <EOL> '<STR_LIT>' : _s3 . get_bucket ( settings . AWS_S3_BUILDS_BUCKET ) , <EOL> } <EOL> else : <EOL> _s3 = None <EOL> _buckets = None <EOL> def _requires_aws ( fn ) : <EOL> if settings . AWS_ENABLED : <EOL> return fn <EOL> else : <EOL> def complain ( * args , ** kwargs ) : <EOL> raise Exception ( "<STR_LIT>" % fn . __name__ ) <EOL> return complain <EOL> @ _requires_aws <EOL> def read_file ( bucket_name , path ) : <EOL> bucket = _buckets [ bucket_name ] <EOL> key = bucket . get_key ( path ) <EOL> return key . get_contents_as_string ( ) <EOL> @ _requires_aws <EOL> def read_file_to_filesystem ( bucket_name , path , destination ) : <EOL> bucket = _buckets [ bucket_name ] <EOL> key = bucket . get_key ( path ) <EOL> key . get_contents_to_filename ( destination ) <EOL> @ _requires_aws <EOL> def delete_file ( bucket_name , path ) : <EOL> bucket = _buckets [ bucket_name ] <EOL> key = bucket . get_key ( path ) <EOL> key . delete ( ) <EOL> @ _requires_aws <EOL> def save_file ( bucket_name , path , value , public = False , content_type = '<STR_LIT>' ) : <EOL> bucket = _buckets [ bucket_name ] <EOL> key = Key ( bucket ) <EOL> key . key = path <EOL> if public : <EOL> policy = '<STR_LIT>' <EOL> else : <EOL> policy = '<STR_LIT>' <EOL> key . set_contents_from_string ( value , policy = policy , headers = { '<STR_LIT:Content-Type>' : content_type } ) <EOL> @ _requires_aws <EOL> def upload_file ( bucket_name , dest_path , src_path , public = False , content_type = '<STR_LIT>' , download_filename = None ) : <EOL> bucket = _buckets [ bucket_name ] <EOL> key = Key ( bucket ) <EOL> key . key = dest_path <EOL> if public : <EOL> policy = '<STR_LIT>' <EOL> else : <EOL> policy = '<STR_LIT>' <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : content_type <EOL> } <EOL> if download_filename is not None : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % download_filename . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> key . set_contents_from_filename ( src_path , policy = policy , headers = headers ) <EOL> @ _requires_aws <EOL> def get_signed_url ( bucket_name , path , headers = None ) : <EOL> bucket = _buckets [ bucket_name ] <EOL> key = bucket . get_key ( path ) <EOL> url = key . generate_url ( <NUM_LIT> , response_headers = headers ) <EOL> if '<STR_LIT>' in url : <EOL> url = url . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return url </s>
