-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from l-iberty/master
UT补充、安全专项、重试逻辑
- Loading branch information
Showing
36 changed files
with
3,789 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding=utf-8 | ||
from qcloud_cos import CosConfig | ||
from qcloud_cos import CosS3Client | ||
import sys | ||
import os | ||
import logging | ||
|
||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
# 1. 设置用户属性, 包括 secret_id, secret_key, region 等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 | ||
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket | ||
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 | ||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 | ||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 | ||
|
||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) | ||
client = CosS3Client(config) | ||
|
||
# 设置存储桶 ACL | ||
response = client.put_bucket_acl( | ||
Bucket='examplebucket-1250000000', | ||
ACL='public-read' | ||
) | ||
|
||
# 查询存储桶 ACL | ||
response = client.get_bucket_acl( | ||
Bucket='examplebucket-1250000000' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding=utf-8 | ||
from qcloud_cos import CosConfig | ||
from qcloud_cos import CosS3Client | ||
import sys | ||
import os | ||
import logging | ||
|
||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
# 1. 设置用户属性, 包括 secret_id, secret_key, region 等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 | ||
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket | ||
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 | ||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 | ||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 | ||
|
||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) | ||
client = CosS3Client(config) | ||
|
||
# 设置跨域配置 | ||
response = client.put_bucket_cors( | ||
Bucket='examplebucket-1250000000', | ||
CORSConfiguration={ | ||
'CORSRule': [ | ||
{ | ||
'ID': 'string', | ||
'MaxAgeSeconds': 100, | ||
'AllowedOrigin': [ | ||
'string', | ||
], | ||
'AllowedMethod': [ | ||
'string', | ||
], | ||
'AllowedHeader': [ | ||
'string', | ||
], | ||
'ExposeHeader': [ | ||
'string', | ||
] | ||
} | ||
] | ||
}, | ||
) | ||
|
||
# 查询跨域配置 | ||
response = client.get_bucket_cors( | ||
Bucket='examplebucket-1250000000', | ||
) | ||
|
||
# 删除跨域配置 | ||
response = client.delete_bucket_cors( | ||
Bucket='examplebucket-1250000000', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding=utf-8 | ||
from qcloud_cos import CosConfig | ||
from qcloud_cos import CosS3Client | ||
import sys | ||
import os | ||
import logging | ||
|
||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
# 1. 设置用户属性, 包括 secret_id, secret_key, region 等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 | ||
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket | ||
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 | ||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 | ||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 | ||
|
||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) | ||
client = CosS3Client(config) | ||
|
||
# 设置自定义域名 | ||
response = client.put_bucket_domain( | ||
Bucket='bucket', | ||
DomainConfiguration={ | ||
'DomainRule': [ | ||
{ | ||
'Name': 'example.com', | ||
'Type': 'REST'|'WEBSITE'|'ACCELERATE', | ||
'Status': 'ENABLED'|'DISABLED', | ||
'ForcedReplacement': 'CNAME'|'TXT' | ||
}, | ||
] | ||
} | ||
) | ||
|
||
# 查询自定义域名 | ||
response = client.get_bucket_domain( | ||
Bucket='examplebucket-1250000000' | ||
) | ||
|
||
# 删除自定义域名 | ||
response = client.delete_bucket_domain( | ||
Bucket='examplebucket-1250000000' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# -*- coding=utf-8 | ||
from qcloud_cos import CosConfig | ||
from qcloud_cos import CosS3Client | ||
import sys | ||
import os | ||
import logging | ||
|
||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
# 1. 设置用户属性, 包括 secret_id, secret_key, region 等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 | ||
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket | ||
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 | ||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 | ||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 | ||
|
||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) | ||
client = CosS3Client(config) | ||
|
||
# 设置清单任务 | ||
response = client.put_bucket_inventory( | ||
Bucket='examplebucket-1250000000', | ||
Id='string', | ||
InventoryConfiguration={ | ||
'Destination': { | ||
'COSBucketDestination': { | ||
'AccountId': '100000000001', | ||
'Bucket': 'qcs::cos:ap-guangzhou::examplebucket-1250000000', | ||
'Format': 'CSV', | ||
'Prefix': 'string', | ||
'Encryption': { | ||
'SSECOS': {} | ||
} | ||
} | ||
}, | ||
'IsEnabled': 'true'|'false', | ||
'Filter': { | ||
'Prefix': 'string' | ||
}, | ||
'IncludedObjectVersions':'All'|'Current', | ||
'OptionalFields': { | ||
'Field': [ | ||
'Size', | ||
'LastModifiedDate', | ||
'ETag', | ||
'StorageClass', | ||
'IsMultipartUploaded', | ||
'ReplicationStatus' | ||
] | ||
}, | ||
'Schedule': { | ||
'Frequency': 'Daily'|'Weekly' | ||
} | ||
} | ||
) | ||
|
||
# 查询清单任务 | ||
response = client.get_bucket_inventory( | ||
Bucket='examplebucket-1250000000', | ||
Id='string' | ||
) | ||
|
||
# 列举清单任务 | ||
continuation_token = '' | ||
while True: | ||
resp = client.list_bucket_inventory_configurations( | ||
Bucket='examplebucket-1250000000', | ||
ContinuationToken=continuation_token, | ||
) | ||
if 'InventoryConfiguration' in resp: | ||
for conf in resp['InventoryConfiguration']: | ||
print(conf) | ||
|
||
if resp['IsTruncated'] == 'true': | ||
continuation_token = resp['NextContinuationToken'] | ||
else: | ||
break | ||
|
||
# 删除清单任务 | ||
response = client.delete_bucket_inventory( | ||
Bucket='examplebucket-1250000000', | ||
Id='string' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding=utf-8 | ||
from qcloud_cos import CosConfig | ||
from qcloud_cos import CosS3Client | ||
import sys | ||
import os | ||
import logging | ||
|
||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 | ||
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket | ||
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 | ||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 | ||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 | ||
|
||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) | ||
client = CosS3Client(config) | ||
|
||
# 设置生命周期 | ||
response = client.put_bucket_lifecycle( | ||
Bucket='examplebucket-1250000000', | ||
LifecycleConfiguration={ | ||
'Rule': [ | ||
{ | ||
'ID': 'string', # 设置规则的 ID,例如Rule-1 | ||
'Filter': { | ||
'Prefix': '' # 配置前缀为空,桶内所有对象都会执行此规则 | ||
}, | ||
'Status': 'Enabled', # Enabled 表示启用规则 | ||
'Expiration': { | ||
'Days': 200 # 设置对象的当前版本200天后过期删除 | ||
}, | ||
'Transition': [ | ||
{ | ||
'Days': 100, # 设置对象的当前版本100天后沉降 | ||
'StorageClass': 'Standard_IA' # 沉降为低频存储 | ||
}, | ||
], | ||
'AbortIncompleteMultipartUpload': { | ||
'DaysAfterInitiation': 7 # 设置7天后回收未合并的分块 | ||
} | ||
} | ||
] | ||
} | ||
) | ||
|
||
# 查询生命周期 | ||
response = client.get_bucket_lifecycle( | ||
Bucket='examplebucket-1250000000', | ||
) | ||
|
||
# 删除生命周期 | ||
response = client.delete_bucket_lifecycle( | ||
Bucket='examplebucket-1250000000', | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding=utf-8 | ||
from qcloud_cos import CosConfig | ||
from qcloud_cos import CosS3Client | ||
import sys | ||
import os | ||
import logging | ||
|
||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息 | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 | ||
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket | ||
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224 | ||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048 | ||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 | ||
|
||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) | ||
client = CosS3Client(config) | ||
|
||
# 设置日志管理 | ||
response = client.put_bucket_logging( | ||
Bucket='examplebucket-1250000000', | ||
BucketLoggingStatus={ | ||
'LoggingEnabled': { | ||
'TargetBucket': 'logging-bucket-1250000000', | ||
'TargetPrefix': 'string' | ||
} | ||
} | ||
) | ||
|
||
# 查询日志管理 | ||
response = client.get_bucket_logging( | ||
Bucket='examplebucket-1250000000' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding=utf-8 | ||
from qcloud_cos import CosConfig | ||
from qcloud_cos import CosS3Client | ||
import sys | ||
import os | ||
import logging | ||
|
||
# 正常情况日志级别使用INFO,需要定位时可以修改为DEBUG,此时SDK会打印和服务端的通信信息 | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成 | ||
secret_id = os.environ['COS_SECRET_ID'] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140 | ||
region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket | ||
# COS支持的所有region列表参见https://cloud.tencent.com/document/product/436/6224 | ||
token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048 | ||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填 | ||
|
||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme) | ||
client = CosS3Client(config) | ||
|
||
# 查询存储桶列表 | ||
response = client.list_buckets() | ||
print(response) | ||
|
||
# 创建存储桶 | ||
# 存储桶名称不支持大写字母,COS 后端会将用户传入的大写字母自动转换为小写字母用于创建存储桶 | ||
response = client.create_bucket( | ||
Bucket='examplebucket-1250000000' | ||
) | ||
|
||
# 检索存储桶及其权限 | ||
response = client.head_bucket( | ||
Bucket='examplebucket-1250000000' | ||
) | ||
|
||
# 删除存储桶 | ||
response = client.delete_bucket( | ||
Bucket='examplebucket-1250000000' | ||
) | ||
|
||
# 判断存储桶是否存在 | ||
response = client.bucket_exists( | ||
Bucket='examplebucket-1250000000' | ||
) | ||
print(response) |
Oops, something went wrong.