简介
本文介绍对象存储 COS 通过 Python SDK 实现查询存储桶列表、创建存储桶、检索存储桶、删除存储桶的示例代码和描述。
注意事项
在您使用存储桶相关操作之前,需要先具有相关权限,具体而言:
若想查询存储桶列表,在您进行 授权策略 时,action 需要设置为
cos:GetService
,更多授权请参见 支持 CAM 的业务接口。若想创建存储桶,在您进行 授权策略 时,action 需要设置为
cos:PutBucket
,更多授权请参见 支持 CAM 的业务接口。若想检索存储桶,在您进行 授权策略 时,action 需要设置为
cos:HeadBucket
,更多授权请参见 支持 CAM 的业务接口。若想删除存储桶,在您进行 授权策略 时,action 需要设置为
cos:DeleteBucket
,更多授权请参见 支持 CAM 的业务接口。相关示例
功能名称 | 描述 | 示例代码 |
查询存储桶列表 | 查询指定账号下所有的存储桶列表 | |
创建存储桶 | 在指定账号下创建一个存储桶 | |
检索存储桶 | 判断某个存储桶是否存在 | |
删除存储桶 | 删除指定账号下的空存储桶 |
查询存储桶列表
功能说明
用于查询指定账号下所有存储桶列表(GET Service)。
方法原型
list_buckets()
请求示例
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/37140secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket# COS支持的所有region列表参见https://cloud.tencent.com/document/product/436/6224token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048scheme = '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)
返回结果说明
查询存储桶列表,类型为 dict。
{'Buckets': {'Bucket': [{'Name': 'string','Location': 'string','CreationDate': 'string'},],},'Owner': {'DisplayName': 'string','ID': 'string'}}
参数名称 | 参数描述 | 类型 |
Buckets | 存储桶列表 | Dict |
Bucket | 存储桶列表 | List |
Name | 存储桶名称 | String |
Location | 存储桶所在的地域名 | String |
CreationDate | 存储桶创建的时间 | String |
Owner | 存储桶所有者信息 | Dict |
DisplayName | 存储桶所有者的名字信息 | String |
ID | 存储桶所有者 ID | String |
创建存储桶
功能说明
在指定账号下创建一个存储桶(PUT Bucket)。
方法原型
create_bucket(Bucket, BucketAZConfig=None, **kwargs)
请求示例
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/37140secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket# COS支持的所有region列表参见https://cloud.tencent.com/document/product/436/6224token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)# 存储桶名称不支持大写字母,COS 后端会将用户传入的大写字母自动转换为小写字母用于创建存储桶response = client.create_bucket(Bucket='examplebucket-1250000000')
全部参数请求示例
response = client.create_bucket(Bucket='examplebucket-1250000000',BucketAZConfig='string',ACL='private'|'public-read'|'public-read-write',GrantFullControl='string',GrantRead='string',GrantWrite='string')
参数说明
参数名称 | 参数描述 | 类型 | 是否必填 |
Bucket | 待创建的存储桶名称,由 BucketName-APPID 构成 存储桶名称不支持大写字母,COS 后端会将用户传入的大写字母自动转换为小写字母用于创建存储桶 | String | 是 |
BucketAZConfig | 存储桶 AZ 配置,指定为 'MAZ' 以创建多 AZ 存储桶 | String | 否 |
ACL | 设置存储桶的 ACL,例如 'private','public-read','public-read-write' | String | 否 |
GrantFullControl | 赋予指定账户对存储桶的读写权限,格式为 id="OwnerUin" | String | 否 |
GrantRead | 赋予指定账户对存储桶的读权限,格式为 id="OwnerUin" | String | 否 |
GrantWrite | 赋予指定账户对存储桶的写权限,格式为 id="OwnerUin" | String | 否 |
返回结果说明
该方法返回值为 None。
检索存储桶及其权限
功能说明
检索存储桶是否存在且是否有权限访问(HEAD Bucket)。
方法原型
head_bucket(Bucket)
请求示例
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/37140secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket# COS支持的所有region列表参见https://cloud.tencent.com/document/product/436/6224token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)response = client.head_bucket(Bucket='examplebucket-1250000000')print(response)
参数说明
参数名称 | 参数描述 | 类型 | 是否必填 |
Bucket | 待查询的存储桶名称,由 BucketName-APPID 构成 | String | 是 |
返回结果说明
请求响应信息和存储桶的元信息,类型为 dict:
{'Content-Type': 'application/xml','Content-Length': '0','Connection': 'keep-alive','Date': 'Mon, 04 Nov 2019 11:09:05 GMT','Server': 'tencent-cos','x-cos-bucket-region': 'ap-beijing','x-cos-bucket-az-type': 'MAZ','x-cos-request-id': 'NjcyOGFiNTFfMTg3NWMyMWVfZmQ0OF9hMzc****'}
参数名称 | 参数描述 | 类型 |
Content-Type | 内容类型,HTTP 标准头部 | String |
Content-Length | 请求响应体的大小,HTTP 标准头部 | String |
Connection | 连接方式,HTTP 标准头部 | String |
Server | 服务端标识,HTTP 标准头部 | String |
Date | 请求日期,HTTP 标准头部 | String |
x-cos-bucket-region | String | |
x-cos-bucket-az-type | 存储桶 AZ 类型,当存储桶为多 AZ 存储桶时返回此头部,值固定为 MAZ | String |
x-cos-request-id | 请求 ID | String |
删除存储桶
功能说明
删除指定账号下的空存储桶(DELETE Bucket)。
方法原型
delete_bucket(Bucket)
请求示例
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/37140secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket# COS支持的所有region列表参见https://cloud.tencent.com/document/product/436/6224token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)response = client.delete_bucket(Bucket='examplebucket-1250000000')
参数说明
参数名称 | 参数描述 | 类型 | 是否必填 |
Bucket | 待删除的存储桶名称,由 BucketName-APPID 构成 | String | 是 |
返回结果说明
该方法返回值为 None。
判断存储桶是否存在
功能说明
判断存储桶是否存在。
方法原型
bucket_exists(Bucket)
请求示例
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/37140secret_key = os.environ['COS_SECRET_KEY'] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket# COS支持的所有region列表参见https://cloud.tencent.com/document/product/436/6224token = None # 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)response = client.bucket_exists(Bucket='examplebucket-1250000000')print(response)
参数说明
参数名称 | 参数描述 | 类型 | 是否必填 |
Bucket | 存储桶名称,由 BucketName-APPID 构成 | String | 是 |
返回结果说明
True 表示存储桶存在,False 表示存储桶不存在。
API 操作
关于查询存储桶列表的API 接口说明,请参见 GET Service (List Buckets) 文档。
关于创建存储桶的API 接口说明,请参见 PUT Bucket 文档。
关于检索存储桶的API 接口说明,请参见 HEAD Bucket 文档。
关于删除存储桶的API 接口说明,请参见 DELETE Bucket 文档。