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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(".")[-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_package(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)!= 4 and 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 + " ...")

 

 

参考资料:

官方文档: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

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

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

 

以上是关于Python使用 boto 调用 S3 对象存储API的主要内容,如果未能解决你的问题,请参考以下文章

AWS BOTO3 S3 python - 调用 HeadObject 操作时发生错误(404):未找到

使用Python boto3上传Windows EC2实例中的文件至S3存储桶中

使用boto3批量上传图片到S3

我们可以使用 boto3 Python 在 aws s3 存储桶之间递归复制文件和文件夹吗?

使用 python boto3 将文件从一个 S3 存储桶传输到另一个 S3 存储桶

python Python3 / boto3多线程S3对象删除