Skip to content

Commit ea1e8ee

Browse files
authored
Merge pull request #273 from l-iberty/master
UT补充、安全专项、重试逻辑
2 parents 7d185a6 + 103ab94 commit ea1e8ee

36 files changed

+3789
-311
lines changed

demo/bucket_acl.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
# 设置存储桶 ACL
23+
response = client.put_bucket_acl(
24+
Bucket='examplebucket-1250000000',
25+
ACL='public-read'
26+
)
27+
28+
# 查询存储桶 ACL
29+
response = client.get_bucket_acl(
30+
Bucket='examplebucket-1250000000'
31+
)

demo/bucket_cors.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_cors(
24+
Bucket='examplebucket-1250000000',
25+
CORSConfiguration={
26+
'CORSRule': [
27+
{
28+
'ID': 'string',
29+
'MaxAgeSeconds': 100,
30+
'AllowedOrigin': [
31+
'string',
32+
],
33+
'AllowedMethod': [
34+
'string',
35+
],
36+
'AllowedHeader': [
37+
'string',
38+
],
39+
'ExposeHeader': [
40+
'string',
41+
]
42+
}
43+
]
44+
},
45+
)
46+
47+
# 查询跨域配置
48+
response = client.get_bucket_cors(
49+
Bucket='examplebucket-1250000000',
50+
)
51+
52+
# 删除跨域配置
53+
response = client.delete_bucket_cors(
54+
Bucket='examplebucket-1250000000',
55+
)

demo/bucket_domain.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_domain(
24+
Bucket='bucket',
25+
DomainConfiguration={
26+
'DomainRule': [
27+
{
28+
'Name': 'example.com',
29+
'Type': 'REST'|'WEBSITE'|'ACCELERATE',
30+
'Status': 'ENABLED'|'DISABLED',
31+
'ForcedReplacement': 'CNAME'|'TXT'
32+
},
33+
]
34+
}
35+
)
36+
37+
# 查询自定义域名
38+
response = client.get_bucket_domain(
39+
Bucket='examplebucket-1250000000'
40+
)
41+
42+
# 删除自定义域名
43+
response = client.delete_bucket_domain(
44+
Bucket='examplebucket-1250000000'
45+
)

demo/bucket_inventory.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
)

demo/bucket_lifecycle.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_lifecycle(
24+
Bucket='examplebucket-1250000000',
25+
LifecycleConfiguration={
26+
'Rule': [
27+
{
28+
'ID': 'string', # 设置规则的 ID,例如Rule-1
29+
'Filter': {
30+
'Prefix': '' # 配置前缀为空,桶内所有对象都会执行此规则
31+
},
32+
'Status': 'Enabled', # Enabled 表示启用规则
33+
'Expiration': {
34+
'Days': 200 # 设置对象的当前版本200天后过期删除
35+
},
36+
'Transition': [
37+
{
38+
'Days': 100, # 设置对象的当前版本100天后沉降
39+
'StorageClass': 'Standard_IA' # 沉降为低频存储
40+
},
41+
],
42+
'AbortIncompleteMultipartUpload': {
43+
'DaysAfterInitiation': 7 # 设置7天后回收未合并的分块
44+
}
45+
}
46+
]
47+
}
48+
)
49+
50+
# 查询生命周期
51+
response = client.get_bucket_lifecycle(
52+
Bucket='examplebucket-1250000000',
53+
)
54+
55+
# 删除生命周期
56+
response = client.delete_bucket_lifecycle(
57+
Bucket='examplebucket-1250000000',
58+
)
59+

demo/bucket_logging.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_logging(
24+
Bucket='examplebucket-1250000000',
25+
BucketLoggingStatus={
26+
'LoggingEnabled': {
27+
'TargetBucket': 'logging-bucket-1250000000',
28+
'TargetPrefix': 'string'
29+
}
30+
}
31+
)
32+
33+
# 查询日志管理
34+
response = client.get_bucket_logging(
35+
Bucket='examplebucket-1250000000'
36+
)

demo/bucket_operation.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.list_buckets()
24+
print(response)
25+
26+
# 创建存储桶
27+
# 存储桶名称不支持大写字母,COS 后端会将用户传入的大写字母自动转换为小写字母用于创建存储桶
28+
response = client.create_bucket(
29+
Bucket='examplebucket-1250000000'
30+
)
31+
32+
# 检索存储桶及其权限
33+
response = client.head_bucket(
34+
Bucket='examplebucket-1250000000'
35+
)
36+
37+
# 删除存储桶
38+
response = client.delete_bucket(
39+
Bucket='examplebucket-1250000000'
40+
)
41+
42+
# 判断存储桶是否存在
43+
response = client.bucket_exists(
44+
Bucket='examplebucket-1250000000'
45+
)
46+
print(response)

0 commit comments

Comments
 (0)