|
| 1 | +# -*- coding=utf-8 |
| 2 | +from qcloud_cos import CosConfig |
| 3 | +from qcloud_cos import CosS3Client |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import logging |
| 7 | + |
| 8 | +# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 |
| 9 | +logging.basicConfig(level=logging.INFO, stream=sys.stdout) |
| 10 | + |
| 11 | +# 1. 设置用户属性, 包括 secret_id, secret_key, region 等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 |
| 12 | +secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 |
| 13 | +secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 |
| 14 | +region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket |
| 15 | + # COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 |
| 16 | +token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 |
| 17 | +scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 |
| 18 | + |
| 19 | +config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) |
| 20 | +client = CosS3Client(config) |
| 21 | + |
| 22 | +# 设置清单任务 |
| 23 | +response = client.put_bucket_inventory( |
| 24 | + Bucket='examplebucket-1250000000', |
| 25 | + Id='string', |
| 26 | + InventoryConfiguration={ |
| 27 | + 'Destination': { |
| 28 | + 'COSBucketDestination': { |
| 29 | + 'AccountId': '100000000001', |
| 30 | + 'Bucket': 'qcs::cos:ap-guangzhou::examplebucket-1250000000', |
| 31 | + 'Format': 'CSV', |
| 32 | + 'Prefix': 'string', |
| 33 | + 'Encryption': { |
| 34 | + 'SSECOS': {} |
| 35 | + } |
| 36 | + } |
| 37 | + }, |
| 38 | + 'IsEnabled': 'true'|'false', |
| 39 | + 'Filter': { |
| 40 | + 'Prefix': 'string' |
| 41 | + }, |
| 42 | + 'IncludedObjectVersions':'All'|'Current', |
| 43 | + 'OptionalFields': { |
| 44 | + 'Field': [ |
| 45 | + 'Size', |
| 46 | + 'LastModifiedDate', |
| 47 | + 'ETag', |
| 48 | + 'StorageClass', |
| 49 | + 'IsMultipartUploaded', |
| 50 | + 'ReplicationStatus' |
| 51 | + ] |
| 52 | + }, |
| 53 | + 'Schedule': { |
| 54 | + 'Frequency': 'Daily'|'Weekly' |
| 55 | + } |
| 56 | + } |
| 57 | +) |
| 58 | + |
| 59 | +# 查询清单任务 |
| 60 | +response = client.get_bucket_inventory( |
| 61 | + Bucket='examplebucket-1250000000', |
| 62 | + Id='string' |
| 63 | +) |
| 64 | + |
| 65 | +# 列举清单任务 |
| 66 | +continuation_token = '' |
| 67 | +while True: |
| 68 | + resp = client.list_bucket_inventory_configurations( |
| 69 | + Bucket='examplebucket-1250000000', |
| 70 | + ContinuationToken=continuation_token, |
| 71 | + ) |
| 72 | + if 'InventoryConfiguration' in resp: |
| 73 | + for conf in resp['InventoryConfiguration']: |
| 74 | + print(conf) |
| 75 | + |
| 76 | + if resp['IsTruncated'] == 'true': |
| 77 | + continuation_token = resp['NextContinuationToken'] |
| 78 | + else: |
| 79 | + break |
| 80 | + |
| 81 | +# 删除清单任务 |
| 82 | +response = client.delete_bucket_inventory( |
| 83 | + Bucket='examplebucket-1250000000', |
| 84 | + Id='string' |
| 85 | +) |
0 commit comments