更新时间:2026-07-13 GMT+08:00
分享

删除对象accesslabel(C SDK)

开发过程中,您有任何问题可以在GitHub上提交issue,或者在华为云对象存储服务论坛中发帖求助。

OBS支持为对象删除AccessLabel进行权限控制处理,您可以通过函数delete_access_label删除并行文件系统中对象的accesslabel。

接口约束

  • 您必须是对象拥有者或拥有设置对象accesslabel的权限,才能调用本接口。建议使用IAM或桶策略进行授权,相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略配置对象策略
  • OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点
  • 仅支持并行文件系统的目录级别的设置,并行文件系统级和文件级均不支持。
  • 目前接口仅在并行文件系统支持,普通对象桶不支持,如何创建并行文件系统请参考创建并行文件系统

方法定义

1
2
3
void delete_access_label(const obs_options *options, char* key,
	obs_response_handler *handler,
	void* callback_data);

请求参数解释

表1 请求参数列表

参数名称

参数类型

是否必选

描述

options

const obs_options *

必选

参数解释:

请求桶的上下文,通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。

约束限制:

取值范围:

默认取值:

key

char *

必选

参数解释:

并行文件系统中的目录名称。

约束限制:

同一个并行文件系统中存储的目录名必须是唯一的。

取值范围:

长度大于0且不超过1024的字符串。

默认取值:

handler

obs_response_handler *

必选

参数解释:

回调结构体,结构体内所有成员都是回调函数的指针,用于设置处理接口响应数据的回调函数。您可以通过设置回调函数,把服务端的响应数据复制到您的自定义回调数据callback_data中。

约束限制:

取值范围:

默认取值:

callback_data

void *

可选

参数解释:

用户自定义回调数据。

约束限制:

取值范围:

默认取值:

代码示例

以下示例展示如何删除对象的accesslabel:
  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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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);
int main()
{
    // 以下示例展示如何通过函数set_access_label删除并行文件系统中对象的accesslabel:
    // 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
    obs_status ret_status = obs_initialize(OBS_INIT_ALL);
    if (OBS_STATUS_OK != ret_status)
    {
        printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
        return -1;
    }
    obs_options options;
    // 创建并初始化options,该参数包括访问域名(host_name)、访问密钥(access_key_id和acces_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-posix-bucket-name。
    char * bucket_name = "example-posix-bucket-name";
    options.bucket_options.bucket_name = bucket_name;
    obs_response_handler handler = {
        response_properties_callback, 
        response_complete_callback  
    };
    char *key = "test_set_access_label_directory/";
    obs_status status = OBS_STATUS_BUTT;
    delete_access_label(&options, key, &handler, &status);
    // 判断请求是否成功
    if (status == OBS_STATUS_OK) {
        printf("delete_access_label for bucket %s successfully. \n", bucket_name);
    }
    else {
        printf("delete_access_label for bucket %s failed(%s).\n", bucket_name, obs_get_status_name(status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
// 响应回调函数,可以在这个回调中把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("expiration", expiration);
    print_nonnull("website_redirect_location", website_redirect_location);
    print_nonnull("version_id", version_id);
    print_nonnull("allow_origin", allow_origin);
    print_nonnull("allow_headers", allow_headers);
    print_nonnull("max_age", max_age);
    print_nonnull("allow_methods", allow_methods);
    print_nonnull("expose_headers", expose_headers);
    print_nonnull("storage_class", storage_class);
    print_nonnull("server_side_encryption", server_side_encryption);
    print_nonnull("kms_key_id", kms_key_id);
    print_nonnull("customer_algorithm", customer_algorithm);
    print_nonnull("customer_key_md5", customer_key_md5);
    print_nonnull("bucket_location", bucket_location);
    print_nonnull("obs_version", obs_version);
    print_nonnull("restore", restore);
    print_nonnull("obs_object_type", obs_object_type);
    print_nonnull("obs_next_append_position", obs_next_append_position);
    print_nonnull("obs_head_epid", obs_head_epid);
    print_nonnull("reserved_indicator", reserved_indicator);
    int i;
    for (i = 0; i < properties->meta_data_count; i++) {
        printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
            properties->meta_data[i].value);
    }
    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) {
        obs_status *ret_status = (obs_status *)callback_data;
        *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);
        }
    }
}

相关链接

  • 关于删除对象accesslabel的API说明,请参见API文档。
  • 设置删除accesslabel中返回的错误码含义、问题原因及处理措施可参考OBS错误码

相关文档