• python 调用 oss sdk


    #cython: language_level=3
    import sys
    import oss2
    import logging
    from itertools import islice
    
    
    OSS_BUCKEET_NAME = "xxxxx"
    OSS_AK_ID = "xxxxxxxxxx"
    OSS_AK_SECRET = "xxxxxxxxxxxx"
    OSS_ENDPOINT = "https://oss-cn-beijing.aliyuncs.com"
    oss2.set_file_logger("./oss.log", 'oss2', logging.INFO)
    auth = oss2.Auth(OSS_AK_ID,OSS_AK_SECRET)
    bucket = oss2.Bucket(auth=auth, endpoint=OSS_ENDPOINT, bucket_name=OSS_BUCKEET_NAME)
    
    
    def percentage(consumed_bytes, total_bytes):
        if total_bytes:
            rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
            print('\r{0}% '.format(rate), end='')
            sys.stdout.flush()
    
    
    def download(source_file_list):
    
        try:
            if "," in source_file_list:
                source_file_list = source_file_list.split(",")
                for source_file in source_file_list:
                    path = source_file.split("/")[-1]
                    oss2.resumable_download(bucket, source_file,  path, progress_callback=percentage)
            else:
                path = source_file_list.split("/")[-1]
                oss2.resumable_download(bucket, source_file_list, path, progress_callback=percentage)
        except oss2.exceptions as e:
            print('status={0}, request_id={1}'.format(e.status, e.request_id))
    
    
    def list_file():
        for b in islice(oss2.ObjectIterator(bucket=bucket, prefix='test/'), 20):
            print(b.key)
    
    
    if __name__ == '__main__':
        help = """
        HELP:
        ====================================================================================
        list file: Usage: Example: ./oss list
        Download one file: Usage: Example: ./oss test/test.txt 
        Download muti files: Usage: Example: ./oss test/test111.txt,test/test222.txt 
        ====================================================================================
        """
        print(help)
        if sys.argv[1] == "list":
            list_file()
        else:
            download(sys.argv[1])
  • 相关阅读:
    Linux
    Linux
    JavaScript
    JavaScript
    Linux
    不可不说的Java“锁”事
    RabbitMQ公共配置
    求一个数字的补码
    项目中Controller的全局异常处理类
    如何较方便给上百张数据库表添加表字段
  • 原文地址:https://www.cnblogs.com/fengzi7314/p/16394317.html
Copyright © 2020-2023  润新知