Updated on 2024-04-29 GMT+08:00

Performing a Conditioned Download

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

When downloading an object, you can specify one or more conditions. Only when the conditions are met, the object will be downloaded. Otherwise, an exception indicating a download failure will be thrown.

You can set the following conditions:

Parameter

Description

obs_get_conditions.if_modified_since

Returns the object if it has been modified since the specified time; otherwise, an error is returned.

obs_get_conditions.if_not_modified_since

Returns the object if it has not been modified since the specified time; otherwise, an error is returned.

obs_get_conditions.if_match_etag

Returns the source object if its ETag is the same as the one specified by this parameter; otherwise, an exception is thrown.

obs_get_conditions.if_not_match_etag

Returns the source object if its ETag is different from the one specified by this parameter; otherwise, an exception is thrown.

  • The ETag of an object is the MD5 check value of the object.
  • If the download request includes if_not_modified_since or If-Match and the specified condition is not met, an exception will be thrown with HTTP status code 412 precondition failed returned.
  • If a request includes if_modified_since or If-None-Match, and the specified condition is not met, 304 Not Modified will be returned.

Sample code:

static void test_get_object_by_range()
{
    // Create and initialize option.
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.bucket_options.bucket_name = "<Your bucketname>";

    // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/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");
    // Range download parameters
    obs_get_conditions getcondition;
    memset(&getcondition, 0, sizeof(obs_get_conditions));
    init_get_properties(&getcondition);
    getcondition.if_match_etag = "<object etag>";
    getcondition.if_modified_since = "<time object modified>";
    getcondition.if_not_match_etag = "<not matched etag>";
    getcondition.if_not_modified_since = "<time object modified>";
    // Download object information.
    obs_object_info object_info;
    memset(&object_info, 0, sizeof(obs_object_info));
    object_info.key = "<object key>";
    object_info.version_id = "<object version ID>";
    // Local file information after download
    get_object_callback_data data;
    data.ret_status = OBS_STATUS_BUTT;
    data.outfile = write_to_file("<file path>");
     // Set response callback function.
    obs_get_object_handler getobjectHandler =
    { 
        { &response_properties_callback, &get_object_complete_callback},
        &get_object_data_callback
    };  
       
    // Download objects.
    get_object(&option,&object_info,&getcondition,0,&getobjectHandler,&data);
    fclose(data.outfile);
    if (OBS_STATUS_OK == data.ret_status) {
        printf("get object successfully. \n");
    }
    else
    {
        printf("get object faied(%s).\n", obs_get_status_name(data.ret_status));
    }
}