<s> from boto . s3 . connection import S3Connection <EOL> from boto . s3 . key import Key <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from mediasync import TYPES_TO_COMPRESS <EOL> from mediasync . backends import BaseClient <EOL> from mediasync . conf import msettings <EOL> import mediasync <EOL> import datetime <EOL> class Client ( BaseClient ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Client , self ) . __init__ ( * args , ** kwargs ) <EOL> self . aws_bucket = msettings [ '<STR_LIT>' ] <EOL> self . aws_prefix = msettings . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( '<STR_LIT:/>' ) <EOL> self . aws_bucket_cname = msettings . get ( '<STR_LIT>' , False ) <EOL> assert self . aws_bucket <EOL> def supports_gzip ( self ) : <EOL> return msettings . get ( '<STR_LIT>' , True ) <EOL> def get_connection ( self ) : <EOL> return self . _conn <EOL> def open ( self ) : <EOL> key = msettings [ '<STR_LIT>' ] <EOL> secret = msettings [ '<STR_LIT>' ] <EOL> try : <EOL> self . _conn = S3Connection ( key , secret ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> self . _bucket = self . _conn . create_bucket ( self . aws_bucket ) <EOL> def close ( self ) : <EOL> self . _bucket = None <EOL> self . _conn = None <EOL> def remote_media_url ( self , with_ssl = False ) : <EOL> """<STR_LIT>""" <EOL> protocol = '<STR_LIT:http>' if with_ssl is False else '<STR_LIT>' <EOL> url = ( self . aws_bucket_cname and "<STR_LIT>" or "<STR_LIT>" ) % ( protocol , self . aws_bucket ) <EOL> if self . aws_prefix : <EOL> url = "<STR_LIT>" % ( url , self . aws_prefix ) <EOL> return url <EOL> def put ( self , filedata , content_type , remote_path , force = False ) : <EOL> now = datetime . datetime . utcnow ( ) <EOL> then = now + datetime . timedelta ( self . expiration_days ) <EOL> expires = then . strftime ( "<STR_LIT>" ) <EOL> if self . aws_prefix : <EOL> remote_path = "<STR_LIT>" % ( self . aws_prefix , remote_path ) <EOL> ( hexdigest , b64digest ) = mediasync . checksum ( filedata ) <EOL> raw_b64digest = b64digest <EOL> headers = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:Content-Type>" : content_type , <EOL> "<STR_LIT>" : expires , <EOL> "<STR_LIT>" : '<STR_LIT>' % ( self . expiration_days * <NUM_LIT> * <NUM_LIT> ) , <EOL> } <EOL> key = self . _bucket . get_key ( remote_path ) <EOL> if key is None : <EOL> key = Key ( self . _bucket , remote_path ) <EOL> key_meta = key . get_metadata ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> s3_checksum = key_meta . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:+>' ) <EOL> if force or s3_checksum != raw_b64digest : <EOL> key . set_metadata ( '<STR_LIT>' , raw_b64digest ) <EOL> key . set_contents_from_string ( filedata , headers = headers , md5 = ( hexdigest , b64digest ) ) <EOL> if content_type in TYPES_TO_COMPRESS : <EOL> key = Key ( self . _bucket , "<STR_LIT>" % remote_path ) <EOL> filedata = mediasync . compress ( filedata ) <EOL> ( hexdigest , b64digest ) = mediasync . checksum ( filedata ) <EOL> headers [ "<STR_LIT>" ] = '<STR_LIT>' % remote_path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> headers [ "<STR_LIT>" ] = '<STR_LIT>' <EOL> key . set_metadata ( '<STR_LIT>' , raw_b64digest ) <EOL> key . set_contents_from_string ( filedata , headers = headers , md5 = ( hexdigest , b64digest ) ) <EOL> return True </s>
