对象存储服务 OBS-设置生命周期规则:设置对象过期时间

时间:2024-10-09 15:40:14

设置对象过期时间

以下代码展示了如何设置最新版本对象和历史版本对象的过期时间:

static void test_set_bucket_lifecycle_configuration2()
{
    obs_options option;
    obs_status  ret_status = OBS_STATUS_BUTT;
    // 设置option
    init_obs_options(&option);
    option.bucket_options.host_name = HOST_NAME;
    option.bucket_options.bucket_name = bucket_name;

    //从环境变量读取ak/sk
    option.bucket_options.access_key = getenv("AC CES S_KEY_ID");
    option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 设置完成的回调函数
    obs_response_handler response_handler =
    { 
        NULL, &response_complete_callback
    };
    obs_lifecycle_conf bucket_lifecycle_conf;
    memset(&bucket_lifecycle_conf, 0, sizeof(obs_lifecycle_conf)); 
    //生命周期规则的id
    bucket_lifecycle_conf.id = "test1"; 
    // 指定前缀"test"
    bucket_lifecycle_conf.prefix = "test"; 
    // 指定满足前缀的对象创建10天后过期 
    bucket_lifecycle_conf.days = "10"; 
    // 指定满足前缀的对象的历史版本20天后过期 
    bucket_lifecycle_conf.noncurrent_version_days = "20";
    // 该生命周期规则生效
    bucket_lifecycle_conf.status = "Enabled"; 
    set_bucket_lifecycle_configuration(&option, &bucket_lifecycle_conf, 1, 
                &response_handler, &ret_status);
    if (OBS_STATUS_OK == ret_status) {
         printf("set bucket lifecycle configuration success.\n");
    }
    else
    {
        printf("set bucket lifecycle configuration failed(%s).\n",
                obs_get_status_name(ret_status));
    }
}
support.huaweicloud.com/sdk-c-devg-obs/obs_20_0902.html