安装 SDK
安装 SDK 有三种安装方式:pip 安装、手动安装和离线安装。
python
pip install -U cos-python-sdk-v5
对象存储手册
该示例展示通过组合 SDK 的基本接口,完成批量上传本地文件夹到 COS。
# -*- coding=utf-8 from qcloud_cos import CosConfig from qcloud_cos import CosS3Client from qcloud_cos import CosServiceError from qcloud_cos import CosClientError from qcloud_cos.cos_threadpool import SimpleThreadPool import sys import os import logging # 设置用户属性, 包括secret_id, secret_key, region # APPID 已在配置中移除,请在参数 Bucket 中带上 APPID。Bucket 由 BucketName-APPID 组成 secret_id = '' # 替换为用户的 secret_id secret_key = '' # 替换为用户的 secret_key region = 'ap-guangzhou' # 替换为用户的 region token = None # 使用临时密钥需要传入 Token,默认为空,可不填 config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 client = CosS3Client(config) uploadDir = '/root/logs' bucket = 'examplebucket-125000000' g = os.walk(uploadDir) # 创建上传的线程池 pool = SimpleThreadPool() for path, dir_list, file_list in g: for file_name in file_list: srcKey = os.path.join(path, file_name) cosObjectKey = srcKey.strip('/') # 判断COS上文件是否存在 exists = False try: response = client.head_object(Bucket=bucket, Key=cosObjectKey) exists = True except CosServiceError as e: if e.get_status_code() == 404: exists = False else: print("Error happened, reupload it.") if not exists: print("File %s not exists in cos, upload it", srcKey) pool.add_task(client.upload_file, bucket, cosObjectKey, srcKey) pool.wait_completion() result = pool.get_result() if not result['success_all']: print("Not all files upload sucessed. you should retry")