|
| 1 | +# -*- coding=utf-8 |
| 2 | +from qcloud_cos import CosConfig |
| 3 | +from qcloud_cos import CosS3Client |
| 4 | +from qcloud_cos import CosServiceError |
| 5 | +from qcloud_cos import CosClientError |
| 6 | +from qcloud_cos.cos_threadpool import SimpleThreadPool |
| 7 | + |
| 8 | +import sys |
| 9 | +import os |
| 10 | +import logging |
| 11 | + |
| 12 | +# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x |
| 13 | + |
| 14 | +# pip安装指南:pip install -U cos-python-sdk-v5 |
| 15 | + |
| 16 | +# cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224 |
| 17 | + |
| 18 | +logging.basicConfig(level=logging.INFO, stream=sys.stdout) |
| 19 | + |
| 20 | +# 设置用户属性, 包括secret_id, secret_key, region |
| 21 | +# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成 |
| 22 | +secret_id = '' # 替换为用户的secret_id |
| 23 | +secret_key = '' # 替换为用户的secret_key |
| 24 | +region = 'ap-guangzhou' # 替换为用户的region |
| 25 | +token = None # 使用临时密钥需要传入Token,默认为空,可不填 |
| 26 | +config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 |
| 27 | +client = CosS3Client(config) |
| 28 | + |
| 29 | +bucket = 'examplebucket-1250000000' |
| 30 | + |
| 31 | +# 删除目录 |
| 32 | +# 对象存储中,目录是特殊的路径以‘/’结尾的object。调用Delete Object接口即可 |
| 33 | +try: |
| 34 | + to_delete_dir = 'path/to/delete/dir/' |
| 35 | + response = client.delete_object( |
| 36 | + Bucket=bucket, |
| 37 | + Key=to_delete_dir, |
| 38 | + ) |
| 39 | + print(response) |
| 40 | +except CosServiceError as e: |
| 41 | + print(e.get_status_code()) |
| 42 | + |
| 43 | +uploadDir = '/root/logs' |
| 44 | + |
| 45 | +g = os.walk(uploadDir) |
| 46 | +# 创建上传的线程池 |
| 47 | +pool = SimpleThreadPool() |
| 48 | +for path, dir_list, file_list in g: |
| 49 | + for file_name in file_list: |
| 50 | + srcKey = os.path.join(path, file_name) |
| 51 | + cosObjectKey = srcKey.strip('/') |
| 52 | + # 判断COS上文件是否存在 |
| 53 | + exists = False |
| 54 | + try: |
| 55 | + response = client.head_object(Bucket=bucket, Key=cosObjectKey) |
| 56 | + exists = True |
| 57 | + except CosServiceError as e: |
| 58 | + if e.get_status_code() == 404: |
| 59 | + exists = False |
| 60 | + else: |
| 61 | + print("Error happened, reupload it.") |
| 62 | + if not exists: |
| 63 | + print("File %s not exists in cos, upload it", srcKey) |
| 64 | + pool.add_task(client.upload_file, bucket, cosObjectKey, srcKey) |
| 65 | + |
| 66 | +pool.wait_completion() |
| 67 | +result = pool.get_result() |
| 68 | +if not result['success_all']: |
| 69 | + print("Not all files upload sucessed. you should retry") |
| 70 | + |
| 71 | +# 删除指定前缀 (prefix)的文件 |
| 72 | +is_over = False |
| 73 | +marker = '' |
| 74 | +prefix = 'root/logs' |
| 75 | +while not is_over: |
| 76 | + try: |
| 77 | + response = client.list_objects(Bucket=bucket, Prefix=prefix, Marker=marker) |
| 78 | + if response['Contents']: |
| 79 | + for content in response['Contents']: |
| 80 | + print("delete object: ", content['Key']) |
| 81 | + client.delete_object(Bucket=bucket, Key=content['Key']) |
| 82 | + |
| 83 | + if response['IsTruncated'] == 'false': |
| 84 | + is_over = True |
| 85 | + marker = response['Marker'] |
| 86 | + |
| 87 | + except CosServiceError as e: |
| 88 | + print(e.get_origin_msg()) |
| 89 | + print(e.get_digest_msg()) |
| 90 | + print(e.get_status_code()) |
| 91 | + print(e.get_error_code()) |
| 92 | + print(e.get_error_msg()) |
| 93 | + print(e.get_resource_location()) |
| 94 | + print(e.get_trace_id()) |
| 95 | + print(e.get_request_id()) |
| 96 | + break |
| 97 | + |
| 98 | +# 移动对象 |
| 99 | +srcKey = 'demo.py' # 原始的对象路径 |
| 100 | +destKey = 'dest_object_key' # 目的对象路径 |
| 101 | + |
| 102 | +try: |
| 103 | + response = client.copy_object( |
| 104 | + Bucket=bucket, |
| 105 | + Key=destKey, |
| 106 | + CopySource={ |
| 107 | + 'Bucket': bucket, |
| 108 | + 'Key': srcKey, |
| 109 | + 'Region': 'ap-guangzhou', |
| 110 | + }) |
| 111 | + client.delete_object(Bucket=bucket, Key=srcKey) |
| 112 | +except CosException as e: |
| 113 | + print(e.get_error_msg()) |
0 commit comments