"Python" calls the S3 object storage API using Boto

Source: Internet
Author: User
Tags assert

code example:

Import logging# fromdjango.conf Import Settingsimport boto fromboto.s3.key Import Keyimport osimport sys##################################################################### # # #user="XXX"aws_access_key_id="XXX"Aws_secret_access_key="XXX"S3_host="XXX"Deploy_package= user +"_deploy_package"Update_package= user +"_update_package"####################################################################### #logger=Logging.getlogger (__name__)classS3_storage (Object): def __init__ (self): Self.conn=Boto.connect_s3 (aws_access_key_id=aws_access_key_id, Aws_secret_access_key=Aws_secret_access_key, Host=S3_host, Is_secure=False, Calling_format='Boto.s3.connection.OrdinaryCallingFormat') Self.deploy_package_bucket_name=deploy_package Self.update_package_bucket_name=update_package Self.deploy_package_bucket=Self.conn.get_bucket (self.deploy_package_bucket_name) Self.update_package_bucket=Self.conn.get_bucket (self.update_package_bucket_name) def upload_package (self, Package_type, Package_path): Package_name=os.path.basename (package_path) #type= Package_name.split (".")[-1]        if(Package_type = ="Deploy"): Key=Key (Self.deploy_package_bucket, Package_name) elif (Package_type=="Update"): Key=Key (Self.update_package_bucket, Package_name) Key.set_contents_from_filename (package_path) def Download_pac Kage (self, package_type, filename, dst_path): #type= Filename.split (".")[-1]        if(Package_type = ="Deploy"): Key=self.deploy_package_bucket.get_key (filename) elif (Package_type=="Update"): Key=self.update_package_bucket.get_key (filename) key.get_contents_to_filename (Dst_path+"/"+filename)if__name__ = ="__main__":    if(Len (sys.argv)! =4and Len (SYS.ARGV)! =5): Print ("=========================================================================================================== =========") Print ("| Require:pip Install boto==2.43.0") Print ("| Usage:python s3_storage.py <upload> <deploy | update> <package_path>") Print ("| Python s3_storage.py <download> <deploy | update> <filename> <dst_path>") Print ("| Example:python s3_storage.py upload deploy Xxx.run") Print ("| Python s3_storage.py download deploy Xxx.run.") Print ("| Python s3_storage.py upload update xxx.run") Print ("| Python s3_storage.py download update xxx.run.") Print ("=========================================================================================================== =========") Sys.exit (-1) elif (len (SYS.ARGV)==4): Type= sys.argv[1] Assert (type=="Upload") Package_type= sys.argv[2] Package_path= sys.argv[3] Package_name=os.path.basename (package_path) s3_storage=s3_storage () print ("UPLOAD Package"+ Package_name +"To S3 START ...") S3_storage.upload_package (Package_type, Package_path) print ("UPLOAD Package SUCCESS ...") elif (len (SYS.ARGV)==5): Type= sys.argv[1] Assert (type=="Download") Package_type= sys.argv[2] FileName= sys.argv[3] Dst_path= sys.argv[4] S3_storage=s3_storage () print ("DOWNLOAD Package"+ filename +"From S3 START ...") s3_storage.download_package (filename, dst_path) print ("DOWNLOAD Package SUCCESS to"+ Dst_path +"/"+ filename +" ...")

Resources:

Official Document: Http://boto.cloudhackers.com/en/latest/s3_tut.html

Http://stackoverflow.com/questions/26415923/boto-get-md5-s3-file

Http://www.cnblogs.com/yxpblog/p/5332162.html

Recommendation: https://www.douban.com/note/315118595/

Http://www.cnblogs.com/asmot/p/3939151.html

"Python" calls the S3 object storage API using Boto

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.