更新时间:2024-12-03 GMT+08:00

删除多版本对象

开发过程中,您有任何问题可以在github上提交issue

删除单个多版本对象

您可以通过delete_object接口传入版本号(version_id)删除多版本对象,示例代码如下:

static void test_delete_object_version()
{
    obs_status ret_status = OBS_STATUS_BUTT;
    // 创建并初始化对象信息
    obs_object_info object_info;
    memset(&object_info, 0, sizeof(obs_object_info));
    object_info.key = key;
    object_info.version_id = version_id;
    // 创建并初始化option
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.bucket_options.bucket_name = "<Your bucketname>";

    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/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");
    // 设置响应回调函数
    obs_response_handler resqonseHandler =
    { 
        &response_properties_callback,
        &response_complete_callback 
    };
    // 删除对象
    delete_object(&option,&object_info,&resqonseHandler, &ret_status);
    if (OBS_STATUS_OK == ret_status) 
    {
        printf("delete object successfully. \n");
    }
    else
    {
        printf("delete object failed(%s).\n", obs_get_status_name(ret_status));
    }
}

批量删除多版本对象

您可以通过batch_delete_objects接口传入每个待删除对象的版本号(version_id)批量删除多版本对象,示例代码如下:

static void test_batch_delete_object_version()
{
    obs_status ret_status = OBS_STATUS_BUTT;
    // 创建并初始化option
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.bucket_options.bucket_name = "<Your bucketname>";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/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");

    // 初始化待删对象信息
    obs_object_info objectinfo[100];
    objectinfo[0].key = "obj1";
    objectinfo[0].version_id = "versionid1";
    objectinfo[1].key = "obj2";
    objectinfo[1].version_id = "versionid2";
    obs_delete_object_info delobj;
    memset_s(&delobj,sizeof(obs_delete_object_info),0,sizeof(obs_delete_object_info));
    delobj.keys_number = 2;
    // 设置响应回调函数
    obs_delete_object_handler handler =
    { 
        {&response_properties_callback, &response_complete_callback},
        &delete_objects_data_callback
    };
    // 批量删除对象
    batch_delete_objects(&option, objectinfo, &delobj, 0, &handler, &ret_status);
    if (OBS_STATUS_OK == ret_status) {
        printf("test batch_delete_objects successfully. \n");
    }
    else
    {
        printf("test batch_delete_objects faied(%s).\n", obs_get_status_name(ret_status));
    }
}