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

Performing a Streaming Upload

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

In a streaming upload, your data is transferred to callback_data, and the callback function put_object_data_callback defined in the obs_put_object_handler structure is used to process the transferred callback data. Call put_object to implement streaming upload.

Parameter Description

Field

Type

Mandatory or Optional

Description

option

The context of the bucket. For details, see Configuring option.

Mandatory

Bucket parameter

key

char *

Mandatory

Object name

content_length

uint64_t

Mandatory

Length of the object content

put_properties

obs_put_properties*

Mandatory

Properties of the uploaded object

encryption_params

server_side_encryption_params *

Optional

Encryption setting of the uploaded object

handler

obs_put_object_handler *

Mandatory

Callback function

callback_data

void *

Optional

Callback data

Sample Code

static void test_put_object_from_buffer()
{
     // Buffer to be uploaded
    char *buffer = "abcdefg";
     // Length of the buffer to be uploaded
    int buffer_size = strlen(buffer);
     // Name of an object to be uploaded
    char *key = "put_buffer_test";

    // 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>";
    // 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_buffer_object_callback_data data;
    memset(&data, 0, sizeof(put_buffer_object_callback_data));
    // Assign the buffer value to the structure.
    data.put_buffer = buffer;
    // Set buffersize.
    data.buffer_size = buffer_size;

    // Set callback function.
    obs_put_object_handler putobjectHandler =
    { 
        { &response_properties_callback, &put_buffer_complete_callback },
          &put_buffer_data_callback
    };

    put_object(&option, key, buffer_size, &put_properties,0,&putobjectHandler,&data);
    if (OBS_STATUS_OK == data.ret_status) {
        printf("put object from buffer successfully. \n");
    }
    else
    {
        printf("put object from buffer failed(%s).\n",     
                           obs_get_status_name(data.ret_status));
    }
}
  • The content to be uploaded cannot exceed 5 GB.
  • The fifth parameter in put_object is used to provide the server-side encryption. For details, see section Server-Side Encryption.