<s> import os <EOL> import sys <EOL> import boto <EOL> from boto . s3 . key import Key <EOL> from boto . s3 . connection import S3Connection <EOL> class S3Utility ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , access_key , secret_key , s3_bucket ) : <EOL> self . access_key = access_key <EOL> self . secret_key = secret_key <EOL> self . s3_bucket = s3_bucket <EOL> self . _conn = None <EOL> @ property <EOL> def conn ( self ) : <EOL> if self . _conn is not None : <EOL> return self . _conn <EOL> else : <EOL> return S3Connection ( self . access_key , self . secret_key ) <EOL> def download_file_list ( self , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> bucket = self . conn . get_bucket ( self . s3_bucket ) <EOL> file_list = [ ] <EOL> for key in bucket . list ( ) : <EOL> key_name = key . name . encode ( '<STR_LIT:utf-8>' ) <EOL> if key_name . startswith ( prefix ) : <EOL> file_list . append ( key_name ) <EOL> return file_list <EOL> def download_file ( self , file_to_load , local_save_dir ) : <EOL> """<STR_LIT>""" <EOL> bucket = self . conn . get_bucket ( self . s3_bucket ) <EOL> key = Key ( bucket , file_to_load ) <EOL> key_name = key . name . encode ( '<STR_LIT:utf-8>' ) <EOL> save_location = os . path . join ( local_save_dir , key_name ) <EOL> try : <EOL> key . get_contents_to_filename ( save_location ) <EOL> except boto . exception . S3ResponseError as e : <EOL> raise boto . exception . S3ResponseError ( "<STR_LIT>" . format ( key_name ) ) <EOL> return save_location <EOL> def upload_file ( self , filename_to_save_as , file_path ) : <EOL> """<STR_LIT>""" <EOL> def percent_cb ( complete , total ) : <EOL> sys . stdout . write ( '<STR_LIT:.>' ) <EOL> sys . stdout . flush ( ) <EOL> bucket = self . conn . get_bucket ( self . s3_bucket ) <EOL> key = Key ( bucket ) <EOL> key . key = filename_to_save_as <EOL> key . set_contents_from_filename ( file_path , cb = percent_cb , num_cb = <NUM_LIT:50> ) </s>
