To use OSS SDK for C, you must initialize request options (oss_request_options_t) and specify an endpoint.

For more information about endpoints, see Regions and endpoints and Bind custom domain names.

Use an OSS endpoint to initialize request options

The following code provides an example on how to initialize request options by using the OSS domain name:

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize the aos_string_t type. */
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* Specify whether to use CNAME to access OSS. A value of 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Specify network parameters. The second parameter specifies the ownership of ctl. The default value is 0. */
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main() {
    aos_pool_t *p;
    oss_request_options_t *options;
    /* Initialize global variables. */
    if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
        return -1;
    }
    /* Initialize the memory pool and options. */
    aos_pool_create(&p, NULL);
    options = oss_request_options_create(p);
    init_options(options);
    /* Business logic code, which is not listed here. */
    /* Release the memory pool, which releases memory resources allocated for the request. */
    aos_pool_destroy(p);
    /* Release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}
Note For more information about the configuration of request options, see curl_easy_setopt.

Use a custom domain name to initialize request options

The following code provides an example on how to initialize request options by using a custom domain name:

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourCustomEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
void init_options(oss_request_options_t *options) {
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize the aos_string_t type. */
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* Enable CNAME and bind the custom domain name to the bucket. */
    options->config->is_cname = 1;
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main() {
    aos_pool_t *p;
    oss_request_options_t *options;
    /* Initialize global variables. */
    if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
        return -1;
    }
    /* Initialize the memory pool and options. */
    aos_pool_create(&p, NULL);
    options = oss_request_options_create(p);
    init_options(options);
    /* Business logic code, which is not listed here. */
    /* Release the memory pool, which releases memory resources allocated for the request. */
    aos_pool_destroy(p);
    /* Release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

Use STS to initialize request options

The following code provides an example on how to initialize request options by using STS:

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *sts_token = "<yourSecurityToken>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize the aos_string_t type. */
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* Configure STS. */
    aos_str_set(&options->config->sts_token, sts_token);
    /* Specify whether to use CNAME to access OSS. A value of 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main() {
    aos_pool_t *p;
    oss_request_options_t *options;
    /* Initialize global variables. */
    if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
        return -1;
    }
    /* Initialize the memory pool and options. */
    aos_pool_create(&p, NULL);
    options = oss_request_options_create(p);
    init_options(options);
    /* Business logic code, which is not listed here. */
    /* Release the memory pool, which releases memory resources allocated for the request. */
    aos_pool_destroy(p);
    /* Release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

Configure a timeout period

The following code provides an example on how to configure a timeout period:

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize the aos_string_t type. */
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* Specify whether to use CNAME to access OSS. A value of 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Specify network parameters. The second parameter specifies the ownership of ctl. The default value is 0. */
    options->ctl = aos_http_controller_create(options->pool, 0);
    /* Set the value of the connection timeout , which is 10 seconds by default. */
    options->ctl->options->connect_timeout = 10;
    /* Set the value of the DNS timeout , which is 60 seconds by default. */
    options->ctl->options->dns_cache_timeout = 60;
    /* 
    Set the value of the request timeout:
    Set the value of speed_limit to specify the minimum acceptable speed. The default value is 1024, which indicates 1 KB/s.
    Set the value of speed_time to specify the maximum time period during which the minimum speed is acceptable, which is 15 seconds by default.
    The following code indicates that if the transmission speed is lower than 1 KB/s for 15 consecutive seconds, the request times out.
    */
    options->ctl->options->speed_limit = 1024;
    options->ctl->options->speed_time = 15;
}

Configure SSL-based authentication

By default, SSL-based authentication is enabled for OSS SDK for C V3.9.2 or later. If SSL-based authentication fails, you need to set the correct path of the SSL certificate, or disable SSL-based authentication.

The following code provides an example on how to configure SSL-based authentication:

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize the aos_string_t type. */
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* Specify whether to use CNAME. A value of 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Specify network parameters. The second parameter specifies the ownership of ctl. The default value is 0. */
    options->ctl = aos_http_controller_create(options->pool, 0);

    /* Configure SSL-based authentication.
    Set verify_ssl to specify whether to enable SSL-based authentication. Valid values: 0 and 1. Default value: 1, which enables SSL-based authentication.
    Set ca_path to specify the root path of the CA certificate. When verify_ssl is set to 1, this parameter is valid. This parameter is empty by default.
    Set ca_file to specify the path of the CA certificate. When verify_ssl is set to 1, this parameter is valid. This parameter is empty by default. */
    // Enable SSL-based authentication. Set the path of the CA certificate.
    //options->ctl->options->verify_ssl = 1;
    //options->ctl->options->ca_path = "/etc/ssl/certs/";
    //options->ctl->options->ca_file = "/etc/ssl/certs/ca-certificates.crt";

    // Disable SSL-based authentication.
    //options->ctl->options->verify_ssl = 0;
}