Updated on 2024-08-06 GMT+08:00

Creating a Folder

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

OBS does not involve folders like in a file system. All elements in buckets are objects. To create a folder in OBS is essentially to create an object whose size is 0 and whose name ends with a slash (/). Such objects have no difference from other objects and can be downloaded and deleted, except that they are displayed as folders in OBS Console.
static void test_creat_folder()
{    
    char *key = "YourFolder/";    
    // 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");
 
    option.bucket_options.uri_style = OBS_URI_STYLE_PATH;    
    temp_auth_configure tempauth;    
    tempAuthResult  ptrResult;    
    memset(&ptrResult,0,sizeof(tempAuthResult));    
    // Initialize the properties of an object to be uploaded.
    obs_put_properties put_properties;    
    init_put_properties(&put_properties);    
    // Initialize the structure for storing uploaded data.
    put_file_object_callback_data data;    
    memset(&data, 0, sizeof(put_file_object_callback_data));    
    // Set callback function.
    obs_put_object_handler putobjectHandler =    
    {         
        { &response_properties_callback, &put_file_complete_callback },        
        &put_file_data_callback    
    };    
    
    put_object(&option, key, 0, &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));    
    }
}
  • To create a folder in OBS is to create an object whose size is 0 and whose name ends with a slash (/), in essential.
  • To create a multi-level folder, you only need to create the folder with the last level. For example, if you want to create a folder named src1/src2/src3/, create it directly, no matter whether the src1/ and src1/src2/ folders exist.