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

列举并行文件系统(C SDK)

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

您可以通过list_bucket_obs获取并行文件系统列表。该接口可以列举当前账户下所有的并行文件系统,返回结果按照并行文件系统名称的字典顺序排列。

接口约束

  • 您必须拥有obs:bucket:ListAllMyBuckets权限,才能获取并行文件系统列表。建议使用IAM进行授权,配置方式详见使用IAM自定义策略。
  • OBS支持的region以及region与endPoint的对应关系,详细信息请参见地区与终端节点。

方法定义

1
2
void list_bucket_obs(const obs_options *options, obs_list_service_obs_handler *handler, 
                   void *callback_data);

请求参数说明

表1 请求参数列表

字段名

类型

约束

说明

options

const obs_options *

必选

请求桶的上下文,配置option(C SDK),通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权

handler

obs_list_service_obs_handler *

必选

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

callback_data

void *

可选

用户自定义回调数据。

代码示例

以下示例展示如何列举并行文件系统:

#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// 自定义回调数据:用于在回调间传递结果状态
typedef struct list_service_data
{
    int headerPrinted;
    int allDetails;
    obs_status ret_status;
} list_service_data;
// 响应回调函数,可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数,可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void list_bucket_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
// 列举桶的数据回调函数,每列举到一个桶会被回调一次
obs_status listServiceObsCallback(const char *owner_id, const char *bucket_name,
                                  int64_t creation_date_seconds, const char *location,
                                  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");
    list_service_data data;
    memset(&data, 0, sizeof(list_service_data));
    // 自定义响应回调函数
    obs_list_service_obs_handler listHandler =
    {
        {NULL,
        &list_bucket_complete_callback },
        &listServiceObsCallback
    };
    // 列举并行文件系统
    list_bucket_obs(&options, &listHandler, &data);
    if (data.ret_status == OBS_STATUS_OK)
    {
        printf("list bucket successfully. \n");
    }
    else
    {
        printf("list bucket failed(%s).\n", obs_get_status_name(data.ret_status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
// 列举桶的数据回调函数,每列举到一个桶会被回调一次
obs_status listServiceObsCallback(const char *owner_id, const char *bucket_name,
                                  int64_t creation_date_seconds, const char *location,
                                  void *callback_data)
{
    list_service_data *data = (list_service_data *)callback_data;
    if (data && !data->headerPrinted) {
        data->headerPrinted = 1;
        printf("%-56s  %-24s  %-20s\n", "BucketName", "CreationDate", "Location");
    }
    char timebuf[256] = {0};
    if (creation_date_seconds >= 0) {
        time_t t = (time_t)creation_date_seconds;
        strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t));
    }
    else {
        timebuf[0] = 0;
    }
    printf("%-56s  %-24s  %-20s", bucket_name ? bucket_name : "",
           timebuf, location ? location : "");
    if (data && data->allDetails) {
        printf("  %-64s", owner_id ? owner_id : "");
    }
    printf("\n");
    return OBS_STATUS_OK;
}
// 结束回调函数,可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void list_bucket_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    list_service_data *data = (list_service_data *)callback_data;
    if (data) {
        data->ret_status = status;
    }
    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);
        }
    }
}
// 响应回调函数,可以在这个回调中把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;
}

相关链接

  • 关于列举并行文件系统的API说明,请参见列举并行文件系统
  • 更多列举并行文件系统返回的错误码含义、问题原因及处理措施可参考OBS错误码。

相关文档