Skip to content

Commit 103ab94

Browse files
author
libertyzhu
committed
bucket operation demo
1 parent 8bf07af commit 103ab94

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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)