<s> import uuid <EOL> import boto <EOL> from boto . sqs . message import RawMessage <EOL> from boto . exception import SQSDecodeError <EOL> class BigMessage ( RawMessage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queue = None , body = None , s3_url = None ) : <EOL> self . s3_url = s3_url <EOL> super ( BigMessage , self ) . __init__ ( queue , body ) <EOL> def _get_bucket_key ( self , s3_url ) : <EOL> bucket_name = key_name = None <EOL> if s3_url : <EOL> if s3_url . startswith ( '<STR_LIT>' ) : <EOL> s3_components = s3_url [ <NUM_LIT:5> : ] . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> bucket_name = s3_components [ <NUM_LIT:0> ] <EOL> if len ( s3_components ) > <NUM_LIT:1> : <EOL> if s3_components [ <NUM_LIT:1> ] : <EOL> key_name = s3_components [ <NUM_LIT:1> ] <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> raise SQSDecodeError ( msg , self ) <EOL> return bucket_name , key_name <EOL> def encode ( self , value ) : <EOL> """<STR_LIT>""" <EOL> bucket_name , key_name = self . _get_bucket_key ( self . s3_url ) <EOL> if bucket_name and key_name : <EOL> return self . s3_url <EOL> key_name = uuid . uuid4 ( ) <EOL> s3_conn = boto . connect_s3 ( ) <EOL> s3_bucket = s3_conn . get_bucket ( bucket_name ) <EOL> key = s3_bucket . new_key ( key_name ) <EOL> key . set_contents_from_file ( value ) <EOL> self . s3_url = '<STR_LIT>' % ( bucket_name , key_name ) <EOL> return self . s3_url <EOL> def _get_s3_object ( self , s3_url ) : <EOL> bucket_name , key_name = self . _get_bucket_key ( s3_url ) <EOL> if bucket_name and key_name : <EOL> s3_conn = boto . connect_s3 ( ) <EOL> s3_bucket = s3_conn . get_bucket ( bucket_name ) <EOL> key = s3_bucket . get_key ( key_name ) <EOL> return key <EOL> else : <EOL> msg = '<STR_LIT>' % s3_url <EOL> raise SQSDecodeError ( msg , self ) <EOL> def decode ( self , value ) : <EOL> self . s3_url = value <EOL> key = self . _get_s3_object ( value ) <EOL> return key . get_contents_as_string ( ) <EOL> def delete ( self ) : <EOL> if self . s3_url : <EOL> key = self . _get_s3_object ( self . s3_url ) <EOL> key . delete ( ) <EOL> super ( BigMessage , self ) . delete ( ) </s>
