更新时间:2023-11-09 GMT+08:00

查看桶日志配置

开发过程中,您有任何问题可以在github上提交issue

您可以通过get_bucket_logging_configuration查看桶日志配置。以下代码展示了如何查看桶日志配置:

参数描述

字段名

类型

约束

说明

option

请求桶的上下文,配置option

必选

桶参数。

logging_message_data

bucket_logging_message *

必选

当前桶日志管理配置情况,bucket_logging_message说明请参看下表。

handler

obs_response_handler*

必选

回调函数。

callback_data

void *

可选

回调数据。

当前桶日志管理配置结构bucket_logging_message描述如下表:

字段名

类型

说明

target_bucket

char *

在生成日志时,源桶的owner可以指定一个目标桶,将生成的所有日志放到该桶中。在OBS系统中,支持多个源桶生成的日志放在同一个目标桶中,如果这样做,就需要指定不同的target_prefix以达到为来自不同源桶的日志分类的目的。

target_bucket_size

int

target_bucket总大小。

target_prefix

char *

通过该元素可以指定一个前缀给一类日志生成的对象。

target_prefix_size

int

target_prefix总大小。

acl_grants

obs_acl_grant *

权限信息结构体指针。

acl_grant_count

int *

返回的acl_grants个数的指针。

agency

char *

产生logging日志桶Owner创建委托OBS上传logging日志的委托名。

agency_size

int

agency总大小。

示例代码

static void test_get_bucket_logging_configuration()
{
    obs_status ret_status = OBS_STATUS_BUTT;
    // 创建并初始化option
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.bucket_options.bucket_name = "<Your bucketname>";

    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/usermanual-ca/ca_01_0003.html
    option.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 设置响应回调函数
    obs_response_handler response_handler = 
    {
         &response_properties_callback, &response_complete_callback
    };
    // 初始化日志配置结构体
    bucket_logging_message logging_message;
    init_bucket_get_logging_message(&logging_message);
    // 获取桶日志配置
    get_bucket_logging_configuration(&option, &response_handler, &logging_message, &ret_status);
    if (OBS_STATUS_OK == ret_status) 
    {
        if (logging_message.target_bucket) 
        {
            printf("Target_Bucket: %s\n", logging_message.target_bucket);
            if ( logging_message.target_prefix) 
            {
                printf("Target_Prefix: %s\n",  logging_message.target_prefix);
            }
            if (logging_message.agency && logging_message.agency[0] != '\0')             
            {                
                printf(" Agency: %s\n",  logging_message.agency);            
            }
            print_grant_info(*logging_message.acl_grant_count, logging_message.acl_grants);
        }
        else 
        {
            printf("Service logging is not enabled for this bucket.\n");
        }
    }
    else
    {
        printf("get bucket logging faied(%s).\n", obs_get_status_name(ret_status));
    }
    // 销毁日志配置结构体
    destroy_logging_message(&logging_message);
}