标签管理
更新时间:2026-09-22
应用场景
支持对 Bucket 绑定标签信息,方便根据标签做 Bucket 的资源管理。
设置标签
Python
1import os
2
3from baidubce.auth.bce_credentials import BceCredentials
4from baidubce.bce_client_configuration import BceClientConfiguration
5from baidubce.services.bos.bos_client import BosClient
6
7config = BceClientConfiguration(
8 credentials=BceCredentials("your-access-key", "your-secret-key"),
9 endpoint="https://bj.bcebos.com",
10)
11bos_client = BosClient(config)
12
13bucket_name = b"your-bucket-name"
14tag_conf = {
15 "tags": [
16 {
17 "tagKey": "key1",
18 "tagValue": "value123"
19 },
20 {
21 "tagKey": "ttt2",
22 "tagValue": "6863gerg"
23 }
24 ]
25}
26
27response = bos_client.put_bucket_tagging(bucket_name, tag_conf=tag_conf)
28print(response.metadata)
参数说明
| 参数 | 必填 | 说明 |
|---|---|---|
bucket_name |
是 | Bucket 名称。示例值 your-bucket-name 需替换为已存在的 Bucket 名称。 |
tag_conf |
是 | Bucket 标签配置。 |
tags |
是 | 标签列表。 |
tagKey |
是 | 标签键。 |
tagValue |
是 | 标签值。 |
返回值说明:
| 返回值 | 说明 |
|---|---|
response |
SDK 响应对象。 |
response.metadata |
响应元信息,可用于查看请求时间、服务端请求 ID 等信息。 |
获取标签
Python
1import os
2
3from baidubce.auth.bce_credentials import BceCredentials
4from baidubce.bce_client_configuration import BceClientConfiguration
5from baidubce.services.bos.bos_client import BosClient
6
7config = BceClientConfiguration(
8 credentials=BceCredentials("your-access-key", "your-secret-key"),
9 endpoint="https://bj.bcebos.com",
10)
11bos_client = BosClient(config)
12
13bucket_name = b"your-bucket-name"
14response = bos_client.get_bucket_tagging(bucket_name)
15print(response.tag)
参数说明
| 参数 | 必填 | 说明 |
|---|---|---|
bucket_name |
是 | Bucket 名称。示例值 your-bucket-name 需替换为已存在且已绑定标签的 Bucket 名称。 |
返回值说明:
| 返回值 | 说明 |
|---|---|
response |
SDK 响应对象。 |
response.tag |
Bucket 标签列表,列表元素包含标签键和标签值。 |
response.group |
标签分组信息。 |
返回结果示例:
JSON
1[
2 {
3 "tagKey": "ttt2",
4 "tagValue": "6863gerg"
5 },
6 {
7 "tagKey": "key1",
8 "tagValue": "value123"
9 }
10]
标签顺序由服务端返回决定。比对标签时,建议按标签键和值进行归一化比对。
删除标签
Python
1import os
2
3from baidubce.auth.bce_credentials import BceCredentials
4from baidubce.bce_client_configuration import BceClientConfiguration
5from baidubce.services.bos.bos_client import BosClient
6
7config = BceClientConfiguration(
8 credentials=BceCredentials("your-access-key", "your-secret-key"),
9 endpoint="https://bj.bcebos.com",
10)
11bos_client = BosClient(config)
12
13bucket_name = b"your-bucket-name"
14response = bos_client.delete_bucket_tagging(bucket_name)
15print(response.metadata)
参数说明
| 参数 | 必填 | 说明 |
|---|---|---|
bucket_name |
是 | Bucket 名称。示例值 your-bucket-name 需替换为已存在且已绑定标签的 Bucket 名称。 |
返回值说明:
| 返回值 | 说明 |
|---|---|
response |
SDK 响应对象。 |
response.metadata |
响应元信息,可用于查看请求时间、服务端请求 ID 等信息。 |
评价此篇文章
