Help Center/ Object Storage Service/ SDK Reference/ C/ Bucket APIs (SDK for C)/ Bucket Encryption (SDK for C)/ Obtaining the Bucket Encryption Configuration (SDK for C)
Updated on 2026-08-03 GMT+08:00

Obtaining the Bucket Encryption Configuration (SDK for C)

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

Function

If encryption is enabled for a bucket, objects uploaded to it will be encrypted based on the specified encryption method before being stored in OBS. When you download these objects, OBS decrypts them first before returning them to you.

This API is used to obtain the encryption configuration of a bucket.

Restrictions

Method

void get_bucket_encryption(const obs_options *options,
        obs_get_bucket_encryption_handler *handler, void *callback_data);

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

options

const obs_options*

Yes

Explanation:

Context of the requested bucket. You can set the AK, SK, endpoint, bucket, timeout interval, and temporary credentials through obs_options.

Restrictions:

None

Value range:

None

Default value:

None

handler

Table 2 *

Yes

Explanation:

A callback structure where all members are pointers to callback functions, used to set the callback functions that handle response data. You can set a callback function to copy the response data from the server to callback_data (custom callback data).

Restrictions:

None

Value range:

None

Default value:

None

callback_data

void *

No

Explanation:

Custom callback data.

Restrictions:

None

Value range:

None

Default value:

None

Table 2 obs_get_bucket_encryption_handler

Parameter

Type

Mandatory (Yes/No)

Description

response_handler

obs_response_handler

Yes

Explanation:

Response callback function structure.

Restrictions:

None

Value range:

None

Default value:

None

get_bucket_encryption_callback

Table 3*

Yes

Explanation:

Pointer to the callback function of the bucket encryption configuration. You can record the content of encryption_config in callback_data in this callback.

Restrictions:

None

Value range:

None

Default value:

None

Table 3 obs_get_bucket_encryption_callback

Parameter

Type

Mandatory (Yes/No)

Description

encryption_config

Table 4*

Yes

Explanation:

Bucket encryption configuration structure, including information like encryption rules.

Restrictions:

The structure and the memory to which the pointer points will be released by the SDK after the callback. Copy them if you need to keep the data.

Value range:

None

Default value:

None

callback_data

void *

Yes

Explanation:

Pointer to the custom callback data.

Restrictions:

None

Value range:

None

Default value:

None

Table 4 obs_server_side_encryption_rule

Parameter

Type

Mandatory (Yes/No)

Description

sse_algorithm

char *

Yes

Explanation:

Server-side encryption algorithm.

Restrictions:

None

Value range:

  • AES256 (SSE-OBS encryption)
  • kms (SSE-KMS encryption)

Default value:

None

kms_data_encryption

char *

No

Explanation:

Encryption algorithm used in SSE-KMS mode.

Restrictions:

This parameter is valid only when sse_algorithm is set to kms.

Value range:

SM4

Default value:

If this parameter is not specified, AES256 is used by default.

kms_master_key_id

char *

No

Explanation:

ID of the KMS master key used in SSE-KMS mode.

Restrictions:

This parameter is valid only when sse_algorithm is set to kms. If this parameter is not specified, the default master key is used.

Value range:

None

Default value:

None

project_id

char *

No

Explanation:

ID of the project where the KMS master key belongs in SSE-KMS mode.

Restrictions:

This parameter is valid only when sse_algorithm is set to kms. If this parameter is not specified, the default project ID is used.

Value range:

None

Default value:

None

Sample Code

This example obtains the encryption configuration of a bucket.
 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
#include "eSDKOBS.h"
#include <stdio.h>
#include <string.h>
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
obs_status get_encryption_callback(obs_server_side_encryption_configuration *encryption_config, void *callback_data);
typedef struct EncryptionInfo
{
    char sse_algorithm[64];
    char kms_master_key_id[256];
    obs_status ret_status;
} EncryptionInfo;
int main()
{
    obs_initialize(OBS_INIT_ALL);
    obs_options options;
    init_obs_options(&options);
    // Enter the endpoint corresponding to the bucket for host_name. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    options.bucket_options.host_name = "obs.ap-southeast-1.myhuaweicloud.com";
    // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
    // In this example, the AK and SK are stored in environment variables for identity authentication. Before running the code in this example, configure local environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    options.bucket_options.bucket_name = "example-bucket-name";
    obs_response_handler response_handler = {&response_properties_callback, &response_complete_callback};
    obs_get_bucket_encryption_handler get_encryption_handler = {
        response_handler,
        &get_encryption_callback
    };
    EncryptionInfo encryption_info = {0};
    encryption_info.ret_status = OBS_STATUS_BUTT;
    get_bucket_encryption(&options, &get_encryption_handler, &encryption_info);
    if (OBS_STATUS_OK == encryption_info.ret_status) {
        printf("get bucket encryption successfully. sse_algorithm: %s\n", encryption_info.sse_algorithm);
        if (encryption_info.kms_master_key_id[0]) {
            printf("kms_master_key_id: %s\n", encryption_info.kms_master_key_id);
        }
    } else {
        printf("get bucket encryption failed(%s).\n", obs_get_status_name(encryption_info.ret_status));
    }
    obs_deinitialize();
}
obs_status get_encryption_callback(obs_server_side_encryption_configuration *encryption_config, void *callback_data)
{
    EncryptionInfo *info = (EncryptionInfo *)callback_data;
    if (encryption_config && encryption_config->rule) {
        if (encryption_config->rule->sse_algorithm) {
            strncpy(info->sse_algorithm, encryption_config->rule->sse_algorithm, sizeof(info->sse_algorithm) - 1);
            info->sse_algorithm[sizeof(info->sse_algorithm) - 1] = '\0';
        }
        if (encryption_config->rule->kms_master_key_id) {
            strncpy(info->kms_master_key_id, encryption_config->rule->kms_master_key_id, sizeof(info->kms_master_key_id) - 1);
            info->kms_master_key_id[sizeof(info->kms_master_key_id) - 1] = '\0';
        }
    }
    return OBS_STATUS_OK;
}
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    (void)properties; (void)callback_data;
    return OBS_STATUS_OK;
}
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) { ((EncryptionInfo*)callback_data)->ret_status = status; }
    if (error && error->message) { printf("Error: %s\n", error->message); }
}