Source code for awss3tools

# -*- coding: utf-8 -*-
Files create, update, read, delete through AWS S3.
from boto.s3.connection import S3Connection
from cStringIO import InputType
from cStringIO import OutputType
from cStringIO import StringIO

[docs]class AwsS3Tools(S3Connection): ''' AwsS3Tools :param str bucket: s3 bucket Name :param str aws_access_key_id: aws_access_key_id :param str aws_secret_access_key: aws_secret_access_key :param str open_file: filename, the same with s3 object key name. :param bool validate: validate bucket is exist or not. Default is ``False`` for not to call ``get_all_keys``. .. seealso:: ``validate`` in ``get_bucket`` in :class:`boto.s3.connection.S3Connection` :rtype: :class:`boto.s3.connection.S3Connection` :return: :class:`boto.s3.connection.S3Connection` Connect bucket with filename: >>> FILES = AwsS3Tools('toomore-aet', AWSID, AWSKEY, 'toomore.txt') >>> print FILES Connect bucket without filename but using :py:func:`open`: >>> FILES = AwsS3Tools('toomore-aet', AWSID, AWSKEY) >>>'toomore.txt') ''' def __init__(self, bucket, aws_access_key_id, aws_secret_access_key, open_file=None, validate=False): super(AwsS3Tools, self).__init__(aws_access_key_id, aws_secret_access_key) self.bucket = self.get_bucket(bucket, validate=validate) if open_file: else: self.keys = None
[docs] def open(self, filename): ''' Open a file by keyname :param str filename: filename, the same with s3 object key name. >>>'toomore.txt') .. note:: No return value, key object will put into ``self.keys`` ''' get_files = self.bucket.get_key(filename) result = get_files if get_files else self.bucket.new_key(filename) self.keys = result
[docs] def save(self, file_data, make_public=False): ''' Save file to S3 :param file file_data: file data :param bool make_public: to be public :rtype: int :returns: file size in byte. >>> with open('./') as file_data: ... print, True) ''' if isinstance(file_data, (file, InputType, OutputType)): if isinstance(file_data, (InputType, OutputType)): result = self.keys.set_contents_from_file(file_data) elif isinstance(file_data, str): result = self.keys.set_contents_from_string(file_data) else: return type(file_data) if make_public: self.keys.make_public() return result
[docs] def read(self): ''' Read file from S3 :rtype: :py:func:`cStringIO.StringIO` >>> content = >>> print ''' result = StringIO() self.keys.get_contents_to_file(result) return result
[docs] def update(self, file_data, *args, **kwargs): ''' Update file. :param file file_data: file data :rtype: int :returns: file size in byte. >>> content = >>> print >>> content.writelines('Toomore is Toomore') >>> FILES.update(content) >>> print .. todo:: update key with the same acl. ''' return, *args, **kwargs)
[docs] def delete(self): ''' Delete file. ''' return self.keys.delete()
if __name__ == '__main__': import setting #bucket = AwsS3Tools('toomore-aet', setting.ID, setting.KEY).bucket #print bucket.get_all_keys() #print dir(bucket) # ----- create data 1 ----- # #FILES = AwsS3Tools('toomore-aet', setting.ID, setting.KEY, 'toomore.txt') #print FILES #print dir(FILES) # ----- create data 2 ----- # FILES = AwsS3Tools('toomore-aet', setting.ID, setting.KEY)'toomore.txt') # ----- save data ----- # #content = StringIO() #content.write('Toomore is 蔣太多') #print, True) # ----- save files ----- # #with open('./README.rst') as file_data: # print, True) # ----- read files ----- # #content = #print # ----- update files ----- # #content.writelines('Toomore is Toomore') #content.writelines('Toomore is Toomore') #print FILES.update(content) #print # ----- get keys acl ----- # #print dir(FILES.keys) #print dir(FILES.keys.get_acl()) #print FILES.keys.get_acl().to_xml() # ----- delete files ----- # #print FILES.delete() # ----- generate url ----- # #print FILES.keys.generate_url(30)