
# 获取对象标签(C SDK)
![](https://support.huaweicloud.com/sdk-c-devg-obs/public_sys-resources/notice_3.0-zh-cn.png)
开发过程中，您有任何问题可以在GitHub上[提交issue](https://github.com/huaweicloud/huaweicloud-sdk-c-obs/issues)，或者在[华为云对象存储服务论坛](https://bbs.huaweicloud.com/forum/forum-620-1.html)中发帖求助。
#### 功能说明
用户可以通过本接口获取对象（Object）的标签信息。
#### 接口约束
- 您必须是桶拥有者或拥有获取对象标签的权限，才能调用本接口。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:object:GetObjectTagging权限，如果使用桶策略则需授予GetObjectTagging权限。相关授权方式介绍可参见[OBS权限控制概述](https://support.huaweicloud.com/perms-cfg-obs/obs_40_0001.html)，配置方式详见[使用IAM自定义策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0121.html)、[配置对象策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0075.html)。
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/OBS)。
 
#### 方法定义
```
void get_object_tagging(const obs_options *options, const char *key,
                       obs_get_object_tagging_handler *handler, void *callback_data);
```
#### 请求参数说明
表1请求参数列表 
| **参数名称**      | **参数类型**                                                                                                            | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|:---|:---|:---|:---|
| options       | const [obs_options](https://support.huaweicloud.com/sdk-c-devg-obs/obs_20_0085.html#obs_20_0085__table9681927859)\* | 必选       | **参数解释：** 请求桶的上下文，通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                                                                                                                                                                                           |
| key           | char \*                                                                                                             | 必选       | **参数解释：** 对象名。对象名是对象在桶中的完整路径，路径中不包含桶名。 例如，您对象的访问地址为examplebucket.obs.cn-north-4.myhuaweicloud.com/folder/test.txt 中，对象名为folder/test.txt。 **约束限制：** 同一个桶中存储的对象名必须是唯一的。 **取值范围：** 长度大于0且不超过1024的字符串。 **默认取值：** 无 |
| handler       | [obs_get_object_tagging_handler] \*                                                 | 必选       | **参数解释：** 回调结构体，结构体内所有成员都是回调函数的指针，用于设置处理接口响应数据的回调函数。您可以通过设置回调函数，把服务端的响应数据复制到您的自定义回调数据callback_data中。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                                                                                                                                                                                                                  |
| callback_data | void \*                                                                                                             | 可选       | **参数解释：** 用户自定义回调数据。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                        |
   
 表2obs_get_object_tagging_handler 
| **参数名称**                    | **参数类型**                                                              | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                                      |
|:---|:---|:---|:---|
| response_handler            | obs_response_handler\*                                                | 必选       | **参数解释：** 响应回调函数结构体。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                     |
| get_object_tagging_callback | [obs_get_object_tagging_callback] \* | 必选       | **参数解释：** 标签回调函数指针，可以在这个回调中把tagging_count和tagging_list的内容记录到callback_data（用户自定义回调数据）中。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无 |
   
 表3obs_get_object_tagging_callback 
| **参数名称**      | **参数类型**                                              | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| tagging_list  | [obs_name_value]\* | 必选       | **参数解释：** 标签列表。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无         |
| tag_number    | unsigned int                                          | 必选       | **参数解释：** 标签个数。 **约束限制：** 无 **取值范围：** \[0, 10\] **默认取值：** 无 |
| callback_data | void \*                                               | 必选       | **参数解释：** 用户自定义回调数据指针。 **约束限制：** 无 **取值范围：** 无 **默认取值**： 无                                             |
   
 表4obs_name_value 
| **参数名称** | **参数类型** | **是否必选**  | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|:---|
| name     | char \*  | 作为请求参数时必选 | **参数解释：** 标签的键。 **约束限制：** - 同一个桶标签的键不能重复。  - 可以自定义，也可以选择预先在标签管理服务（TMS）创建好的标签。  - 长度范围为1到128个字符。  - 不能以空格开头或结尾，不能包含字符","、"\*"、"\|"、"/"、"\<"、"\>"、"="、"\\"以及ASCII码0x00--0x1F的控制字符，在发送到服务器之前，必须将键名（Key）和键值（Value）进行UrlEncode编码。  - 区分大小写。   **默认取值：** 无 **取值范围：** 无 **默认取值：** 无 |
| value    | char \*  | 作为请求参数时必选 | **参数解释：** 标签的值。 **约束限制：** 标签的值可以重复，并且可以为空。 - 长度范围为0到255个字符。  - 不能以空格开头或结尾，不能包含字符","、"\*"、"\|"、"/"、"\<"、"\>"、"="、"\\"以及ASCII码0x00--0x1F的控制字符，在发送到服务器之前，必须将键名（Key）和键值（Value）进行UrlEncode编码。  - 区分大小写。   **默认取值：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                                                                                                                                           |
   
#### 代码示例：
以下示例展示如何获取对象标签：
```
#include "eSDKOBS.h"
#include <stdio.h>
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
obs_status get_object_tagging_callback(int tagging_count, obs_name_value *tagging_list, void *callback_data);
typedef struct tagkv
{
    char key[250];
    char value[250];
}tagkv;
typedef struct TaggingInfo
{
    int tagCount;
    tagkv taglist[10];
    obs_status ret_status;
}TaggingInfo;
int main()
{
    // 以下示例展示如何通过get_object_tagging获取对象标签：
    // 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
    obs_initialize(OBS_INIT_ALL);
    obs_options options;
    // 创建并初始化options，该参数包括访问域名(host_name)、访问密钥（access_key_id和access_key_secret）、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
    init_obs_options(&options);
    // host_name填写桶所在的endpoint, 此处以华北-北京四为例，其他地区请按实际情况填写。
    options.bucket_options.host_name = "obs.cn-north-4.myhuaweicloud.com";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 填写Bucket名称，例如example-bucket-name。
    char * bucketName = "example-bucket-name";
    options.bucket_options.bucket_name = bucketName;
    obs_response_handler response_handler = { &response_properties_callback, &response_complete_callback };
    // 设置响应回调函数
    obs_get_object_tagging_handler get_object_tagging_handler =
    {
        response_handler,
        &get_object_tagging_callback
    };
    // 创建回调数据
    TaggingInfo tagging_info;
    memset(&tagging_info, 0, sizeof(TaggingInfo));
    tagging_info.ret_status = OBS_STATUS_BUTT;
    // 获取对象标签
    get_object_tagging(&options, "objectname", &get_object_tagging_handler, &tagging_info);
    if (OBS_STATUS_OK == tagging_info.ret_status) {
        printf("get object tagging successfully.\n");
    }
    else
    {
        printf("get object tagging failed(%s).\n", obs_get_status_name(tagging_info.ret_status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
void printTagInfo(TaggingInfo* infoToPrint)
{
    int i;
    printf("tag number is %d\n", infoToPrint->tagCount);
    for (i = 0; i < infoToPrint->tagCount; i++)
    {
        printf("key:[%s], value[%s]\n", infoToPrint->taglist[i].key, infoToPrint->taglist[i].value);
    }
}
obs_status get_object_tagging_callback(int tagging_count, obs_name_value *tagging_list, void *callback_data)
{
    int tag_num = 0;
    TaggingInfo * tag_info = (TaggingInfo *)callback_data;
    tag_info->tagCount = tagging_count;
    if (tagging_count > 0)
    {
        for (tag_num = 0; tag_num < tagging_count; tag_num++)
        {
            memcpy_s(tag_info->taglist[tag_num].key, sizeof(tag_info->taglist[tag_num].key), (&tagging_list[tag_num])->name, strlen((&tagging_list[tag_num])->name) + 1);
            memcpy_s(tag_info->taglist[tag_num].value, sizeof(tag_info->taglist[tag_num].value), (&tagging_list[tag_num])->value, strlen((&tagging_list[tag_num])->value) + 1);
        }
    }
    printTagInfo(tag_info);
    return OBS_STATUS_OK;
}
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    if (properties == NULL)
    {
        printf("error! obs_response_properties is null!");
        return OBS_STATUS_OK;
    }
    // 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
    print_nonnull("request_id", request_id);
    print_nonnull("request_id2", request_id2);
    print_nonnull("content_type", content_type);
    if (properties->content_length) {
        printf("content_length: %llu\n", properties->content_length);
    }
    print_nonnull("server", server);
    print_nonnull("ETag", etag);
    print_nonnull("version_id", version_id);
    return OBS_STATUS_OK;
}
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) {
        TaggingInfo *taggingInfo = (TaggingInfo *)callback_data;
        taggingInfo->ret_status = status;
    }
    else {
        printf("Callback_data is NULL");
    }
    if (error && error->message) {
        printf("Error Message: \n   %s\n", error->message);
    }
    if (error && error->resource) {
        printf("Error Resource: \n  %s\n", error->resource);
    }
    if (error && error->further_details) {
        printf("Error further_details: \n   %s\n", error->further_details);
    }
    if (error && error->extra_details_count) {
        int i;
        for (i = 0; i < error->extra_details_count; i++) {
            printf("Error Extra Detail(%d):\n   %s:%s\n", i, error->extra_details[i].name,
                error->extra_details[i].value);
        }
    }
    if (error && error->error_headers_count) {
        int i;
        for (i = 0; i < error->error_headers_count; i++) {
            const char *errorHeader = error->error_headers[i];
            printf("Error Headers(%d):\n    %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
        }
    }
}
```
#### 相关链接
- 关于获取对象标签的API说明，请参见[获取对象标签](https://support.huaweicloud.com/api-obs/obs_04_0164.html)。
- 获取对象标签过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
 
