Updated on 2026-08-03 GMT+08:00

Obtaining the Default WORM Policy of a Bucket (SDK for C)

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

Function

If a default WORM policy is configured for a bucket, you can call this API to obtain the default WORM policy of the bucket.

If you have never configured the default bucket-level retention policy after enabling WORM for a bucket, you can still use this API to check whether WORM is enabled.

Restrictions

  • To obtain the default WORM policy of a bucket, you must be the bucket owner or have the required permission (obs:bucket:GetBucketObjectLockConfiguration granted using IAM or GetBucketObjectLockConfiguration granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • Before enabling bucket-level WORM, you need to enable versioning.

    WORM protects objects based on the object version IDs. Only object versions with any WORM retention policy configured can be protected. Assume that object test.txt 001 is protected by WORM. If another file with the same name is uploaded, a new object version test.txt 002 with no WORM policy configured will be generated. In such case, test.txt 002 is not protected and can be deleted. If you download an object without specifying a version ID, the current object version (test.txt 002) will be downloaded.

  • A lifecycle rule cannot delete WORM-protected objects, but can transition their storage class. After an object is no longer protected, it will be deleted when meeting the expiration rule in a lifecycle configuration.
  • Once you enable WORM for a bucket, you cannot disable it or suspend versioning for the bucket, but you can disable the default WORM policy for the bucket.
  • If you have deregistered your account or your account has been frozen, the WORM-protected objects will be permanently deleted.
  • WORM-based protection is not available for migration.
  • The metadata of a WORM-protected object can still be modified.
  • Parallel file systems do not support WORM.

Method

void get_bucket_object_lock_configuration(const obs_options *options,
    obs_get_bucket_object_lock_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 1*

Yes

Explanation:

Callback structure, including the response callback, end callback, and callback for obtaining the bucket-level WORM configuration.

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 1 obs_get_bucket_object_lock_handler

Parameter

Type

Mandatory (Yes/No)

Description

handler

obs_response_handler *

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

get_bucket_object_lock_callback

Table 2*

Yes

Explanation:

Pointer to the callback function for obtaining the bucket-level WORM configuration. You can read the content of Table 3 in the callback.

Restrictions:

The pointer to the structure returned by the callback is managed by the SDK. The memory will be released after the callback. If you need to retain the data, copy it in the callback.

Value range:

None

Default value:

None

Table 2 obs_get_bucket_object_lock_callback

Parameter

Type

Mandatory (Yes/No)

Description

object_lock_config_return

Table 3*

Yes

Explanation:

Bucket-level WORM configuration structure, including the WORM switch status and default retention policy.

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 3 obs_bucket_object_lock_configuration

Parameter

Type

Description

object_lock_enabled

char *

Explanation:

WORM switch status.

Restrictions:

None

Value range:

Enabled

Default value:

None

default_retention

Table 4*

Explanation:

Default retention policy. If the value is NULL, no default retention policy is configured.

Restrictions:

None

Value range:

None

Default value:

None

Table 4 obs_worm_default_retention

Parameter

Type

Mandatory (Yes/No)

Description

mode

char *

Yes

Explanation:

Retention mode.

Restrictions:

None

Value range:

COMPLIANCE (In compliance mode, no user can delete protected objects during the retention period.)

Default value:

None

days

unsigned int

No

Explanation:

Number of retention days.

Restrictions:

Either this parameter or years must be set.

Value range:

1 to 36500

Default value:

0

years

unsigned int

No

Explanation:

Number of retention years.

Restrictions:

Either this parameter or days must be set.

Value range:

1 to 100

Default value:

0

Sample Code

This example obtains the default WORM policy of a bucket.

#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);
typedef struct ObjectLockResult
{
    char object_lock_enabled[64];
    char retention_mode[64];
    unsigned int retention_days;
    unsigned int retention_years;
    int has_default_retention;
    obs_status ret_status;
} ObjectLockResult;
obs_status get_object_lock_callback(obs_bucket_object_lock_configuration *config_return, void *callback_data)
{
    ObjectLockResult *result = (ObjectLockResult *)callback_data;
    if (config_return) {
        if (config_return->object_lock_enabled) {
            strncpy(result->object_lock_enabled, config_return->object_lock_enabled,
                    sizeof(result->object_lock_enabled) - 1);
        }
        if (config_return->default_retention) {
            result->has_default_retention = 1;
            if (config_return->default_retention->mode) {
                strncpy(result->retention_mode, config_return->default_retention->mode,
                        sizeof(result->retention_mode) - 1);
            }
            result->retention_days = config_return->default_retention->days;
            result->retention_years = config_return->default_retention->years;
        }
    }
    return OBS_STATUS_OK;
}
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_get_bucket_object_lock_handler handler = {0};
    handler.response_handler.properties_callback = &response_properties_callback;
    handler.response_handler.complete_callback = &response_complete_callback;
    handler.get_bucket_object_lock_callback = &get_object_lock_callback;
    ObjectLockResult result;
    memset(&result, 0, sizeof(ObjectLockResult));
    result.ret_status = OBS_STATUS_BUTT;
    get_bucket_object_lock_configuration(&options, &handler, &result);
    if (OBS_STATUS_OK == result.ret_status) {
        printf("get bucket object lock configuration successfully.\n");
        printf("object_lock_enabled: %s\n", result.object_lock_enabled);
        if (result.has_default_retention) {
            printf("retention_mode: %s\n", result.retention_mode);
            if (result.retention_days > 0) {
                printf("retention_days: %u\n", result.retention_days);
            }
            if (result.retention_years > 0) {
                printf("retention_years: %u\n", result.retention_years);
            }
        } else {
            printf("default_retention: not configured\n");
        }
    } else {
        printf("get bucket object lock configuration failed(%s).\n", obs_get_status_name(result.ret_status));
    }
    obs_deinitialize();
}
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) {
        ObjectLockResult *result = (ObjectLockResult *)callback_data;
        result->ret_status = status;
    }
    if (error && error->message) { printf("Error: %s\n", error->message); }
}

Helpful Links