
# 获取桶级默认WORM策略(C SDK)
![](https://support.huaweicloud.com/sdk-c-devg-obs/public_sys-resources/notice_3.0-zh-cn.png)
开发过程中，您有任何问题可以在GitHub上[提交issue](https://github.com/huaweicloud/huaweicloud-sdk-c-obs/issues)，或者在[华为云对象存储服务论坛](https://bbs.huaweicloud.com/forum/forum-620-1.html)中发帖求助。
#### 功能说明
当桶设置了桶级默认WORM策略，可以通过本接口获取该桶的桶级默认WORM策略。
![](https://support.huaweicloud.com/sdk-c-devg-obs/public_sys-resources/note_3.0-zh-cn.png)
如果您打开了桶级WORM开关，但从未配置过桶级默认WORM策略，您依然可以使用此接口查看开关的打开情况。
#### 接口约束
- 您必须是桶拥有者或拥有获取桶级默认WORM策略的权限，才能调用本接口。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:bucket:GetBucketObjectLockConfiguration权限，如果使用桶策略则需授予GetBucketObjectLockConfiguration权限。相关授权方式介绍可参见[OBS权限控制概述](https://support.huaweicloud.com/perms-cfg-obs/obs_40_0001.html)，配置方式详见[使用IAM自定义策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0121.html)、[自定义创建桶策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0123.html)。
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/OBS)。
- 开启桶级WORM开关前，需要先开启多版本控制。 WORM保护是基于对象版本号的，配置WORM的版本受到WORM保护，没有配置WORM的版本可正常删除。例如，test.txt 001受到WORM保护。此时再次上传同名文件，产生新的对象版本test.txt 002，test.txt 002并未配置WORM，那么test.txt 002就不受保护可以正常删除。当您下载对象时，不指定版本号下载的是最新对象，也就是test.txt 002。
  
- 处于保护中的对象不会被生命周期删除，但是能正常进行存储类别转换。受保护的对象不再受保护后，如果满足生命周期过期规则，将会被系统删除。
- 桶级WORM开关一旦打开，则无法为该桶禁用桶级WORM开关 ，也无法暂停桶的版本控制，但可以禁用桶级默认WORM策略。
- 在客户销户、欠费超期情况下，WORM未到期数据会被删除而不做保留。
- 不支持迁移场景。
- WORM不阻止修改对象元数据。
- 并行文件系统不支持WORM。
 
#### 方法定义
```
void get_bucket_object_lock_configuration(const obs_options *options,
    obs_get_bucket_object_lock_handler *handler, void *callback_data);
```
#### 请求参数说明
**表1** 请求参数列表
| 参数名称          | 参数类型                                                                                                                | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                                                |
|:---|:---|:---|:---|
| options       | const [obs_options](https://support.huaweicloud.com/sdk-c-devg-obs/obs_20_0085.html#obs_20_0085__table9681927859)\* | 必选   | **参数解释：** 请求桶的上下文，通过obs_options设置AK、SK、endpoint、bucket、超时时间、临时鉴权。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无 |
| handler       | [表1 obs_get_bucket_object_lock_handler]\*                                         | 必选   | **参数解释：** 回调结构体，包含响应回调、结束回调和获取桶对象锁定配置的回调。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                        |
| callback_data | void \*                                                                                                             | 可选   | **参数解释：** 用户自定义回调数据。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                        |
   
 表1obs_get_bucket_object_lock_handler 
| 参数名称                            | 参数类型                                                                                                                  | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                                                                                    |
|:---|:---|:---|:---|
| handler                         | [obs_response_handler](https://support.huaweicloud.com/sdk-c-devg-obs/obs_20_0087.html#obs_20_0087__table53755070) \* | 必选   | **参数解释：** 回调结构体，结构体内所有成员都是回调函数的指针，用于设置处理接口响应数据的回调函数。您可以通过设置回调函数，把服务端的响应数据复制到您的自定义回调数据callback_data中。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                |
| get_bucket_object_lock_callback | [表2 obs_get_bucket_object_lock_callback]\*                                           | 必选   | **参数解释：** 获取桶对象锁定配置的回调函数指针，在该回调中可以读取[表1 obs_bucket_object_lock_configuration]的内容。 **约束限制：** 回调返回的结构体指针由SDK内部管理，回调返回后该内存会被释放，如需保留数据请在回调中复制。 **取值范围：** 无 **默认取值：** 无 |
   
 表2obs_get_bucket_object_lock_callback 
| 参数名称                      | 参数类型                                                                        | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                        |
|:---|:---|:---|:---|
| object_lock_config_return | [表1 obs_bucket_object_lock_configuration]\* | 必选   | **参数解释：** 桶对象锁定配置结构体，包含WORM开关状态和默认保留策略。 **约束限制：** 该结构体及其中指针指向的内存在回调返回后会被SDK释放，如需保留请自行复制。 **取值范围：** 无 **默认取值：** 无 |
| callback_data             | void \*                                                                     | 必选   | **参数解释：** 用户自定义回调数据指针。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                    |
   
 表3obs_bucket_object_lock_configuration 
| 参数                  | 参数类型                                                               | 描述                                                                                                                                                                                                                                                                                     |
|:---|:---|:---|
| object_lock_enabled | char \*                                                            | **参数解释：** WORM开关状态。 **约束限制：** 无 **取值范围：** Enabled：启用 **默认取值：** 无 |
| default_retention   | [表1 obs_worm_default_retention]\* | **参数解释：** 默认保留策略。为NULL时表示未配置默认保留策略。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无 |
   
 表4obs_worm_default_retention 
| 参数名称  | 参数类型         | 是否必选 | 描述                                                                                                                                                                                                                                                                                                                                                                                          |
|:---|:---|:---|:---|
| mode  | char \*      | 必选   | **参数解释：** 保护模式。 **约束限制：** 无 **取值范围：** "COMPLIANCE"（合规模式，任何用户都无法在保护期内删除受保护的对象）。 **默认取值：** 无 |
| days  | unsigned int | 可选   | **参数解释：** 保护天数。 **约束限制：** 与years二选一，不可同时设置。 **取值范围：** 1\~36500。 **默认取值：** 0                    |
| years | unsigned int | 可选   | **参数解释：** 保护年数。 **约束限制：** 与days二选一，不可同时设置。 **取值范围：** 1\~100。 **默认取值：** 0                  |
   
#### 代码示例
以下示例展示如何获取桶级默认WORM策略。
```
#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);
   // 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");
    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); }
}
```
#### 相关链接
- 关于获取桶级默认WORM策略的API说明，请参见[获取桶级默认WORM策略](https://support.huaweicloud.com/api-obs/obs_04_0168.html)。
- 获取桶级默认WORM策略过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
- 了解更多配置桶级默认WORM策略相关信息，请参见[配置对象锁定（WORM）防止对象被更改或删除](https://support.huaweicloud.com/usermanual-obs/obs_03_0718.html)。
 
