文档首页/配置审计 Config/API参考/API/资源标签/查询项目标签 - ListTagsForResourceType
更新时间:2026-01-08 GMT+08:00

查询项目标签 - ListTagsForResourceType

功能介绍

查询租户在指定Project中实例类型的所有资源标签集合。标签管理服务需要能够列出当前租户全部已使用的资源标签集合,为各服务Console打资源标签和过滤实例时提供标签联想功能。

说明:该接口仅支持Config的资源类型,当前resource_type支持config:policyAssignments, config:aggregators, config:aggregationAuthorizations。

调用方法

请参见如何调用API

授权信息

账号具备所有API的调用权限,如果使用账号下的IAM用户调用当前API,该IAM用户需具备调用API所需的权限。

  • 如果使用角色与策略授权,具体权限要求请参见权限和授权项
  • 如果使用身份策略授权,需具备如下身份策略权限。

    授权项

    访问级别

    资源类型(*为必须)

    条件键

    别名

    依赖的授权项

    rms::listTags

    List

    -

    -

    rms:resources:listTags

    -

URI

GET /v1/resource-manager/{resource_type}/tags

表1 路径参数

参数

是否必选

参数类型

描述

resource_type

String

资源类型。

取值范围:

  • "config:policyAssignments":Config合规规则

  • "config:aggregators":Config聚合器

  • "config:aggregationAuthorizations":Config聚合器授权

表2 Query参数

参数

是否必选

参数类型

描述

limit

Integer

查询记录数默认为1000,limit最多为1000,不能为负数,最小值为1

最小值:1

最大值:1000

缺省值:1000

offset

Integer

索引位置,从第一条数据偏移offset条数据后开始查询,默认为0(偏移0条数据,表示从第一条数据开始查询),必须为数字,不能为负数

最小值:0

缺省值:0

请求参数

响应参数

状态码:200

表3 响应Body参数

参数

参数类型

描述

tags

Array of Tag objects

标签列表

total_count

Integer

总记录数

表4 Tag

参数

参数类型

描述

key

String

键。最大长度128个unicode字符。 key不能为空。(搜索时不对此参数做字符集校验),key不能为空或者空字符串,不能为空格,校验和使用之前先trim 前后半角空格。

最小长度:1

最大长度:128

values

Array of strings

值列表。每个值最大长度255个unicode字符,校验和使用之前先trim 前后半角空格。value可为空数组但不可缺省。如果values为空列表,则表示any_value(查询任意value)。value之间为或的关系。(搜索时不对此参数做字符集校验,只做长度校验)

最大长度:255

数组长度:0 - 20

状态码:400

表5 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码:401

表6 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码:403

表7 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码:404

表8 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码:500

表9 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

请求示例

查询租户实例类型的所有资源标签集合

GET https://{endpoint}/v1/resource-manager/{resource_type}/tags

响应示例

状态码:200

操作成功。

{
  "tags" : [ {
    "key" : "key1",
    "values" : [ "value1", "value2" ]
  }, {
    "key" : "key2",
    "values" : [ "value1", "value2" ]
  } ],
  "total_count" : 1001
}

SDK代码示例

SDK代码示例如下。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.config.v1.region.ConfigRegion;
import com.huaweicloud.sdk.config.v1.*;
import com.huaweicloud.sdk.config.v1.model.*;


public class ListTagsForResourceTypeSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new GlobalCredentials()
                .withAk(ak)
                .withSk(sk);

        ConfigClient client = ConfigClient.newBuilder()
                .withCredential(auth)
                .withRegion(ConfigRegion.valueOf("<YOUR REGION>"))
                .build();
        ListTagsForResourceTypeRequest request = new ListTagsForResourceTypeRequest();
        request.withResourceType(ListTagsForResourceTypeRequest.ResourceTypeEnum.fromValue("{resource_type}"));
        try {
            ListTagsForResourceTypeResponse response = client.listTagsForResourceType(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import GlobalCredentials
from huaweicloudsdkconfig.v1.region.config_region import ConfigRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkconfig.v1 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]

    credentials = GlobalCredentials(ak, sk)

    client = ConfigClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(ConfigRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = ListTagsForResourceTypeRequest()
        request.resource_type = "{resource_type}"
        response = client.list_tags_for_resource_type(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
    config "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/config/v1"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/config/v1/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/config/v1/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := global.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := config.NewConfigClient(
        config.ConfigClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.ListTagsForResourceTypeRequest{}
	request.ResourceType = model.GetListTagsForResourceTypeRequestResourceTypeEnum().RESOURCE_TYPE
	response, err := client.ListTagsForResourceType(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

操作成功。

400

输入参数不合法。

401

鉴权失败。

403

用户身份认证失败。

404

资源未找到

500

服务器内部错误。

错误码

请参见错误码