【Python】使用 boto 调用 S3 对象存储API

代码示例:

import logging
#from django.conf import settings import boto
from boto.s3.key import Key
import os
import 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__) class S3_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(".")[-]
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_package(self, package_type, filename, dst_path):
#type = filename.split(".")[-]
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)!= and len(sys.argv)!= ):
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(-)
elif( len(sys.argv) == ):
type = sys.argv[]
assert( type=="upload" )
package_type = sys.argv[]
package_path = sys.argv[]
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) == ):
type = sys.argv[]
assert( type=="download" )
package_type = sys.argv[]
filename = sys.argv[]
dst_path = sys.argv[]
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 + " ...")

参考资料:

官方文档:http://boto.cloudhackers.com/en/latest/s3_tut.html

http://*.com/questions/26415923/boto-get-md5-s3-file

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

推荐:https://www.douban.com/note/315118595/

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

上一篇:通过SQL Server Profiler来监视分析死锁


下一篇:View, Activity, Window