更新时间:2026-09-08 GMT+08:00
创建桶(C SDK)
开发过程中,您有任何问题可以在GitHub上提交issue,或者在华为云对象存储服务论坛中发帖求助。
桶是OBS全局命名空间,相当于数据的容器、文件系统的根目录,可以存储若干对象。
以下示例展示如何新建一个对象桶:
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() { // 以下示例展示如何新建一个对象桶: // 在程序入口调用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和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_location需要与endpoint的位置信息一致。 char *bucket_location = "cn-north-4"; // 填写Bucket名称,例如example-bucket-name。 char * bucket_name = "example-bucket-name"; options.bucket_options.bucket_name = bucket_name; obs_response_handler response_handler = { &response_properties_callback, &response_complete_callback }; // 设置桶的存储类别,此处以标准存储为例 options.bucket_options.storage_class = OBS_STORAGE_CLASS_STANDARD; ret_status = OBS_STATUS_BUTT; // 创建桶,并设置桶的ACL权限,此处以设置桶为私有为例 create_bucket(&options, OBS_CANNED_ACL_PRIVATE, bucket_location, &response_handler, &ret_status); // 判断请求是否成功 if (ret_status == OBS_STATUS_OK) { printf("create bucket %s successfully. \n", bucket_name); } else { printf("create bucket %s failed(%s).\n", bucket_name, obs_get_status_name(ret_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); } } } |
桶的名字是全局唯一的,所以您需要确保不与已有的桶名称重复。桶命名规则如下:
- 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。
- 禁止使用IP地址。
- 禁止以“-”或“.”开头及结尾。
- 禁止两个“.”相邻(如:“my..bucket”)。
- 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。
- 同一用户多次创建同名桶不会报错,创建的桶属性以第一次请求为准。
本示例创建的桶ACL为私有读写,存储类别默认是标准类型,区域位置为全局域名所在的默认区域,即华北-北京一(cn-north-1)。
更多创建桶的信息,请参见创建桶(C SDK)。
- 创建桶时,如果使用的终端节点归属于默认区域华北-北京一(cn-north-1),则可以不指定区域;如果使用的终端节点归属于其他区域,则必须指定区域,且指定的区域必须与终端节点归属的区域一致。当前有效的区域名称可从这里查询。比如初始化时使用的终端节点EndPoint是obs.cn-north-4.myhuaweicloud.com,那么在创建桶的时候必须指定Location:cn-north-4才会创建成功,否则会返回状态码400。
父主题: 快速入门(C SDK)