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

Using a User-Defined Domain Name to Access OBS

When using a user-defined domain name to access OBS, you can call APIs as you normally do, except that you need to configure option as shown below:

option.bucket_options.useCname = true;
option.bucket_options.host_name = "yourCustomDomain";

Using a user-defined domain name to upload an object:

static void test_put_object_from_file()
{
    // Name of the object
    char *key = "put_file_test";
    // File to be uploaded
    char file_name[256] = "./put_file_test.txt";
    uint64_t content_length = 0;
    // 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/intl/en-us/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");
    option.bucket_options.certificate_info = "<Your certificate>";

    // Specify a user-defined domain name.
    option.bucket_options.useCname = true;
    option.bucket_options.host_name = "yourCustomDomain";
    // Initialize the properties of the object.
    obs_put_properties put_properties;
    init_put_properties(&put_properties);

    // Initialize the structure for storing data to be uploaded.
    put_file_object_callback_data data;
    memset(&data, 0, sizeof(put_file_object_callback_data));
    // Open the file and obtain the file length.
    content_length = open_file_and_get_length(file_name, &data);
    // Set the callback function.
    obs_put_object_handler putobjectHandler =
    { 
        { &response_properties_callback, &put_file_complete_callback },
        &put_file_data_callback
    };

    put_object(&option, key, content_length, &put_properties, 0, &putobjectHandler, &data);
    if (OBS_STATUS_OK == data.ret_status) {
        printf("put object from file successfully. \n");
    }
    else
    {
        printf("put object failed(%s).\n",  
               obs_get_status_name(data.ret_status));
    }
}

Using a user-defined domain name to download an object:

static void test_get_object()
{
    char *file_name = "./test";
    obs_object_info object_info;
    // 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/intl/en-us/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");

    // Specify a user-defined domain name.
    option.bucket_options.useCname = true;
    option.bucket_options.host_name = "yourCustomDomain";

    // Specify the object to be downloaded.
    memset(&object_info, 0, sizeof(obs_object_info));
    object_info.key = "<object key>";
    object_info.version_id = "<object version ID>";
    //Set the structure for storing downloaded data based on service requirements.
    get_object_callback_data data;
    data.ret_status = OBS_STATUS_BUTT;
    data.outfile = write_to_file(file_name);
    // Define a range to download.
    obs_get_conditions getcondition;
    memset(&getcondition, 0, sizeof(obs_get_conditions));
    init_get_properties(&getcondition);
    getcondition.start_byte = "<start byte>";
    // Define a size to download. The default value is 0, indicating that the object is read to the end.
    getcondition.byte_count = "<byte count>";

    // Define a callback function.
    obs_get_object_handler get_object_handler =
    { 
        { &response_properties_callback,
          &get_object_complete_callback},
        &get_object_data_callback
    };

    get_object(&option, &object_info, &getcondition, 0, &get_object_handler, &data);
    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));
    }
    fclose(data.outfile);
}