Tous les produits
Search
Centre de documentation

Object Storage Service:Initialization (C SDK)

Dernière mise à jour :Aug 18, 2026

Lorsque vous utilisez le SDK OSS pour C, vous devez initialiser les options de requête (oss_request_options_t) et spécifier un endpoint.

Initialiser les options de requête

Utiliser l'algorithme de signature V4 (recommandé)

Nous vous recommandons d'utiliser l'algorithme de signature V4 pour renforcer la sécurité. Lors de l'initialisation du SDK avec la signature V4, vous devez spécifier un endpoint et un ID de région Alibaba Cloud. L'ID de région indique la région vers laquelle la requête est envoyée, par exemple cn-hangzhou. Vous devez également définir signature_version = 4. Les signatures V4 sont prises en charge à partir de la version 3.11.0 du SDK OSS pour C.

L'exemple de code suivant montre comment créer une instance OSSClient à l'aide d'un endpoint OSS et de l'algorithme de signature V4. Pour créer une instance OSSClient à l'aide d'un nom de domaine personnalisé ou d'identifiants d'accès obtenus via STS, adaptez l'exemple de code ci-dessous en conséquence.

#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 region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
const char *region = "yourRegion";

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 data type. */
    aos_str_set(&options->config->endpoint, endpoint);
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */  
    aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
    aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
    // Specify two additional parameters.
    aos_str_set(&options->config->region, region);
    options->config->signature_version = 4;
    /* 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 of the second parameter 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. In this example, the logic code is omitted. */
    /* Release the memory pool. This operation releases the memory resources 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;
}

Utiliser l'algorithme de signature V1 (non recommandé)

Important

Utiliser un endpoint OSS pour initialiser les options de requête

L'exemple de code suivant montre comment utiliser un endpoint OSS pour initialiser les options de requête :

#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";

void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize aos_string_t. */
    aos_str_set(&options->config->endpoint, endpoint);
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */  
    aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
    aos_str_set(&options->config->access_key_secret, getenv("OSS_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 of the second parameter 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. In this example, the logic code is omitted. */
    /* Release the memory pool. This operation releases the memory resources 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;
}

Utiliser un nom de domaine personnalisé pour initialiser les options de requête

L'exemple de code suivant montre comment utiliser un nom de domaine personnalisé pour initialiser les options de requête :

#include "oss_api.h"
#include "aos_http_io.h"
# Specify the custom domain name. */
const char *endpoint = "yourCustomEndpoint";

void init_options(oss_request_options_t *options) {
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize aos_string_t. */
    aos_str_set(&options->config->endpoint, endpoint);
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */  
    aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
    aos_str_set(&options->config->access_key_secret, getenv("OSS_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. In this example, the logic code is omitted. */
    /* Release the memory pool. This operation releases the memory resources 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;
}

Utiliser des identifiants d'accès temporaires fournis par STS pour initialiser les options de requête

L'exemple de code suivant montre comment utiliser des identifiants d'accès temporaires obtenus auprès du Security Token Service (STS) pour initialiser les options de requête :

#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";
/* Before you run the sample code, make sure that the YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET environment variables are configured to store temporary access credentials obtained from STS. */  
const char *access_key_id = getenv("YOUR_ACCESS_KEY_ID");
const char *access_key_secret = getenv("YOUR_ACCESS_KEY_SECRET");
/* 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 aos_string_t. */
    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);
    /* Set the token. */
    aos_str_set(&options->config->sts_token, sts_token);
    /* Specify whether to use CNAME. 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. In this example, the logic code is omitted. */
    /* Release the memory pool. This operation releases the memory resources 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;
}

Paramètres

Le tableau suivant décrit les paramètres courants.

Paramètre

Description

speed_limit

La limite inférieure de la vitesse moyenne. Unité : octets/seconde. Valeur par défaut : 1024.

speed_time

La durée minimale pour le calcul de la vitesse moyenne, en secondes. La valeur par défaut est 15.

Important

Les paramètres speed_limit et speed_time sont tous deux requis. Si la vitesse moyenne reste inférieure à la limite spécifiée par le paramètre speed_limit pendant la durée définie par le paramètre speed_time, la requête est interrompue.

connect_timeout

Le délai d'attente pour l'établissement d'une connexion. Unité : secondes. Valeur par défaut : 10.

dns_cache_timeout

Le délai d'expiration du cache DNS. Unité : secondes. Valeur par défaut : 60.

max_memory_size

La taille maximale des données pouvant être écrites en mémoire lors du téléchargement. Unité : octets. Par défaut, la taille maximale des données pouvant être écrites en mémoire lors du téléchargement est de 1 Go.

enable_crc

Indique s'il faut activer CRC-64. Valeurs possibles :

  • 0 : désactive CRC-64.

  • 1 (par défaut) : active CRC-64.

verify_ssl

Indique s'il faut activer l'authentification basée sur SSL. Valeurs possibles :

  • 0 : désactive l'authentification basée sur SSL.

  • 1 (par défaut) : active l'authentification basée sur SSL.

ca_path

Le chemin racine du certificat CA. Ce paramètre est valide lorsque verify_ssl est défini sur 1. Par défaut, ce paramètre est vide.

ca_file

Le chemin du certificat CA. Ce paramètre est valide lorsque verify_ssl est défini sur 1. Par défaut, ce paramètre est vide.

proxy_host

L'adresse du serveur proxy au format host:port.

proxy_auth

Les identifiants d'authentification pour le serveur proxy au format user:password.

Exemples de configuration

Spécifier un délai d'attente

L'exemple de code suivant montre comment spécifier un délai d'attente :

#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";

void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize aos_string_t. */
    aos_str_set(&options->config->endpoint, endpoint);
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */  
    aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
    aos_str_set(&options->config->access_key_secret, getenv("OSS_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 of the second parameter 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. 
    Configure the speed_limit parameter to specify the lower limit on the average speed. Default value: 1024 (1 KB/s). 
    Configure the speed_time parameter to specify the allowed maximum time period for an average speed that is less than the lower speed limit. Default value: 15. Unit: seconds. 
    The following lines specify that a request timeout 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. In this example, the logic code is omitted. */
    /* Release the memory pool. This operation releases the memory resources 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;
}

Configurer l'authentification basée sur SSL

Par défaut, l'authentification basée sur SSL est activée pour le SDK OSS pour C version 3.9.2 et ultérieures. Si l'authentification basée sur SSL échoue, vérifiez si le chemin du certificat SSL est correct ou désactivez l'authentification basée sur SSL.

L'exemple de code suivant montre comment configurer l'authentification basée sur SSL :

#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";

void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* Use a char* string to initialize aos_string_t. */
    aos_str_set(&options->config->endpoint, endpoint);
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */  
    aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
    aos_str_set(&options->config->access_key_secret, getenv("OSS_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 of the second parameter 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 specifies 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. In this example, the logic code is omitted. */
    /* Release the memory pool. This operation releases the memory resources 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;
}