All Products
Search
Document Center

Object Storage Service:Initialization

Last Updated:Sep 05, 2023

You must initialize request options (oss_request_options_t) and specify an endpoint when you use Object Storage Service (OSS) SDK for C.

Initialize request options

Use an OSS endpoint to initialize request options

The following sample code provides an example on how to use an OSS endpoint to initialize request options:

#include "oss_api.h"
#include "aos_http_io.h"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
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 data of 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. The value 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Specify network parameters. The second parameter in this function specifies the ownership of ctl. By default, the 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. You need to initialize global variables only once in the program lifecycle. */
    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);
    /* The logic code is omitted. */
    /* Release the memory pool. This operation releases memory resources that are allocated for the request. */
    aos_pool_destroy(p);
    /* Release global resources that are allocated. You need to release global resources only once in the program lifecycle. */
    aos_http_io_deinitialize();
    return 0;
}

Use a custom domain name to initialize request options

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

#include "oss_api.h"
#include "aos_http_io.h"
# Specify the custom domain name. */
const char *endpoint = "yourCustomEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
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 data of 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 map the custom domain name to your 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. You need to initialize global variables only once in the program lifecycle. */
    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);
    /* The logic code is omitted. */
    /* Release the memory pool. This operation releases memory resources that are allocated for the request. */
    aos_pool_destroy(p);
    /* Release global resources that are allocated. You need to release global resources only once in the program lifecycle. */
    aos_http_io_deinitialize();
    return 0;
}

Use temporary access credentials that you obtained from STS to initialize request options

The following sample code provides an example on how to use temporary access credentials that are obtained from Security Token Service (STS) to initialize request options:

#include "oss_api.h"
#include "aos_http_io.h"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* Specify the temporary AccessKey pair that is obtained from STS. A temporary AccessKey pair consists of a temporary AccessKey ID and a temporary AccessKey secret. */
const char *access_key_id = "yourAccessKeyId";
const char *access_key_secret = "yourAccessKeySecret";
/* Specify the security token that is obtained from STS. */
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 data of 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. The value 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. You need to initialize global variables only once in the program lifecycle. */
    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);
    /* The logic code is omitted. */
    /* Release the memory pool. This operation releases memory resources that are allocated for the request. */
    aos_pool_destroy(p);
    /* Release global resources that are allocated. You need to release global resources only once in the program lifecycle. */
    aos_http_io_deinitialize();
    return 0;
}

Configure parameters

The following table describes the common parameters.

Parameter

Description

speed_limit

The lower limit for the average speed. Unit: bytes/second. Default value: 1024.

speed_time

The maximum time period for the lower limit of the average speed. Unit: seconds. Default value: 15.

Important

You must configure both the speed_limit and speed_time parameters to specify the timeout period for requests. If the average speed is less than the lower limit that is specified by the speed_limit parameter for a period of time that is specified by the speed_time parameter, the request is interrupted.

connect_timeout

The timeout period for establishing a connection. Unit: seconds. Default value: 10.

dns_cache_timeout

The timeout period of the DNS cache. Unit: seconds. Default value: 60.

max_memory_size

The maximum size of data that can be written to the memory when data is downloaded. Unit: bytes. By default, the maximum size of data that can be written to the memory when data is downloaded is 1 GB.

enable_crc

Specifies whether to enable CRC-64. Valid values:

  • 0: disables CRC-64.

  • 1 (default): enables CRC-64.

verify_ssl

Specifies whether to enable SSL-based authentication. Valid values:

  • 0: disables SSL-based authentication.

  • 1 (default): enables SSL-based authentication.

ca_path

The root path of the CA certificate. This parameter is valid when verify_ssl is set to 1. By default, this parameter is left empty.

ca_file

The path of the CA certificate. This parameter is valid when verify_ssl is set to 1. By default, this parameter is left empty.

proxy_host

The address of the proxy server in the host:port format.

proxy_auth

The password of the proxy server in the user:password format.

Configuration examples

Specify a timeout period

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

#include "oss_api.h"
#include "aos_http_io.h"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
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 data of 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. The value 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Specify network parameters. The second parameter in this function specifies the ownership of ctl. By default, the value is 0. */
    options->ctl = aos_http_controller_create(options->pool, 0);
    /* Specify the timeout period for establishing a connection. Default value: 10. Unit: seconds. */
    options->ctl->options->connect_timeout = 10;
    /* Specify the timeout period of the DNS cache. Default value: 60. Unit: seconds. */
    options->ctl->options->dns_cache_timeout = 60;
    /* 
    Specify the timeout period for requests. 
    Configure the speed_limit parameter to specify the lower limit for the average speed. Default value: 1024 (1 KB/s). 
    Configure the speed_time parameter to specify the maximum time period for the lower limit of the average speed. Default value: 15. Unit: seconds. 
    The following sample code provides an example of a request timeout that occurs when the transmission speed is less than 1 KB/s for 15 consecutive seconds: 
    */
    options->ctl->options->speed_limit = 1024;
    options->ctl->options->speed_time = 15;
}
int main() {
    aos_pool_t *p;
    oss_request_options_t *options;
    /* Initialize global variables. You need to initialize global variables only once in the program lifecycle. */
    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);
    /* The logic code is omitted. */
    /* Release the memory pool. This operation releases memory resources that are allocated for the request. */
    aos_pool_destroy(p);
    /* Release global resources that are allocated. You need to release global resources only once in the program lifecycle. */
    aos_http_io_deinitialize();
    return 0;
}

Configure SSL-based authentication

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

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

#include "oss_api.h"
#include "aos_http_io.h"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
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 data of 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. The value 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Specify network parameters. The second parameter in this function specifies the ownership of ctl. By default, the value is 0. */
    options->ctl = aos_http_controller_create(options->pool, 0);

    /* Configure SSL-based authentication. */
    Configure the verify_ssl parameter to specify whether to enable SSL-based authentication. Valid values: 0 and 1. Default value: 1. The value 1 indicates that SSL-based authentication is enabled. 
    Configure the ca_path parameter to specify the root path of the CA certificate. This parameter is valid when verify_ssl is set to 1. By default, this parameter is left empty. 
    Configure the ca_file parameter to specify the path of the CA certificate. This parameter is valid when verify_ssl is set to 1. By default, this parameter is left empty. */
    /* Enable SSL-based authentication and specify 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;
}
int main() {
    aos_pool_t *p;
    oss_request_options_t *options;
    /* Initialize global variables. You need to initialize global variables only once in the program lifecycle. */
    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);
    /* The logic code is omitted. */
    /* Release the memory pool. This operation releases memory resources that are allocated for the request. */
    aos_pool_destroy(p);
    /* Release global resources that are allocated. You need to release global resources only once in the program lifecycle. */
    aos_http_io_deinitialize();
    return 0;
}