Help Center/ Object Storage Service/ SDK Reference/ C/ Setting Access Logging/ Viewing Bucket Logging Configuration
Updated on 2024-12-03 GMT+08:00

Viewing Bucket Logging Configuration

If you have any questions during development, post them on the Issues page of GitHub.

You can call get_bucket_logging_configuration to view the logging configuration of a bucket. Sample code is as follows:

Parameter Description

Field

Type

Mandatory or Optional

Description

option

The context of the bucket. For details, see Configuring option.

Mandatory

Bucket parameter

logging_message_data

bucket_logging_message *

Mandatory

Current bucket logging management configuration. For details about bucket_logging_message, see the following table.

handler

obs_response_handler*

Mandatory

Callback function

callback_data

void *

Optional

Callback data

The following table describes the bucket logging management configuration structure bucket_logging_message.

Field

Type

Description

target_bucket

char *

When enabling the logging function, the owner of the bucket being logged can specify a target bucket to store the generated log files. Log files generated for multiple buckets can be stored in the same target bucket. If you do so, you need to specify different target_prefix to classify logs for different buckets.

target_bucket_size

int

Total size of target_bucket

target_prefix

char *

You can specify a prefix using this element so that log files are named with this prefix.

target_prefix_size

int

Total size of target_prefix

acl_grants

obs_acl_grant *

Pointer to the permission information structure.

acl_grant_count

int *

Pointer to the number of returned acl_grants

agency

char *

Name of the agency created by the owner of the logging bucket for uploading log files by OBS

agency_size

int

Total size of agency

Sample Code

static void test_get_bucket_logging_configuration()
{
    obs_status ret_status = OBS_STATUS_BUTT;
    // Create and initialize option.
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.bucket_options.bucket_name = "<Your bucketname>";

    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/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");
    // Set response callback function.
    obs_response_handler response_handler = 
    {
         &response_properties_callback, &response_complete_callback
    };
    // Initialize the logging configuration structure.
    bucket_logging_message logging_message;
    init_bucket_get_logging_message(&logging_message);
    // Obtain bucket logs.
    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 the logging configuration structure.
    destroy_logging_message(&logging_message);
}