Object Storage Service (OSS) C SDK'sini kullanırken istek seçeneklerini (oss_request_options_t) başlatmanız ve bir uç nokta belirtmeniz gerekir.
İstek seçeneklerini başlatın
V4 imza algoritmasını kullanın (önerilir)
Geliştirilmiş güvenlik için V4 imza algoritmasını kullanmanızı öneririz. SDK'yı V4 imzasıyla başlatırken bir uç nokta ve bir Alibaba Cloud bölge kimliği belirtmeniz gerekir. Bölge kimliği, isteğin gönderileceği bölgeyi (örneğin cn-hangzhou) tanımlar. Ayrıca signature_version = 4 bildirimini yapmanız gerekir. V4 imzalar, C SDK 3.11.0 ve sonraki sürümlerinde desteklenir.
Aşağıdaki örnek kod, bir OSS uç noktası ve V4 imza algoritması kullanarak bir OSSClient örneğinin nasıl oluşturulacağını gösterir. Özel bir alan adı veya STS'den alınan erişim kimlik bilgileri kullanarak bir OSSClient örneği oluşturmak için aşağıdaki örnek kodu buna göre değiştirin.
#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;
}
V1 imza algoritmasını kullanın (önerilmez)
1 Mart 2025 tarihinden itibaren Alibaba Cloud Object Storage Service (OSS), yeni kullanıcılar (yani yeni UID'ler) için V1 imzasını sunmamaktadır. 1 Eylül 2025 tarihinden itibaren OSS, V1 imzasını güncellemeyi ve bakımayı durduracak ve yeni nesne depolama paketleri için V1 imzasını sunmayacaktır. Hizmet kesintilerini önlemek için mümkün olan en kısa sürede V4 imzasına geçiş yapın. Daha fazla bilgi için bkz. Duyuru.
İstek seçeneklerini başlatmak için bir OSS uç noktası kullanın
Aşağıdaki örnek kod, istek seçeneklerini başlatmak için bir OSS uç noktasının nasıl kullanılacağını gösterir:
#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;
}
İstek seçeneklerini başlatmak için özel bir alan adı kullanın
Aşağıdaki örnek kod, istek seçeneklerini başlatmak için özel bir alan adının nasıl kullanılacağını gösterir:
#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;
}
İstek seçeneklerini başlatmak için STS tarafından sağlanan geçici erişim kimlik bilgilerini kullanın
Aşağıdaki örnek kod, Security Token Service (STS) üzerinden alınan geçici erişim kimlik bilgilerini kullanarak istek seçeneklerinin nasıl başlatılacağını gösterir:
#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;
}
Parametreler
Aşağıdaki tabloda yaygın parametreler açıklanmaktadır.
|
Parametre |
Açıklama |
|
speed_limit |
Ortalama hızın alt sınırı. Birim: bayt/saniye. Varsayılan değer: 1024. |
|
speed_time |
Saniye cinsinden ortalama hız hesaplaması için minimum süre. Varsayılan değer 15'tir. Önemli
Hem speed_limit hem de speed_time parametreleri gereklidir. Ortalama hız, speed_time parametresiyle belirtilen süre boyunca speed_limit parametresiyle belirtilen alt sınırın altında kalırsa istek kesintiye uğrar. |
|
connect_timeout |
Bir bağlantı kurmak için zaman aşımı süresi. Birim: saniye. Varsayılan değer: 10. |
|
dns_cache_timeout |
DNS önbelleği için zaman aşımı süresi. Birim: saniye. Varsayılan değer: 60. |
|
max_memory_size |
Veri indirilirken belleğe yazılabilecek maksimum veri boyutu. Birim: bayt. Varsayılan olarak, veri indirilirken belleğe yazılabilecek maksimum veri boyutu 1 GB'tır. |
|
enable_crc |
CRC-64'ün etkinleştirilip etkinleştirilmeyeceğini belirtir. Geçerli değerler:
|
|
verify_ssl |
SSL tabanlı kimlik doğrulamanın etkinleştirilip etkinleştirilmeyeceğini belirtir. Geçerli değerler:
|
|
ca_path |
CA sertifikasının kök yolu. Bu parametre, verify_ssl değeri 1 olarak ayarlandığında geçerlidir. Varsayılan olarak bu parametre boştur. |
|
ca_file |
CA sertifikasının yolu. Bu parametre, verify_ssl değeri 1 olarak ayarlandığında geçerlidir. Varsayılan olarak bu parametre boştur. |
|
proxy_host |
|
|
proxy_auth |
|
Yapılandırma örnekleri
Bir zaman aşımı süresi belirtin
Aşağıdaki örnek kod, bir zaman aşımı süresinin nasıl belirtileceğini gösterir:
#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;
}
SSL tabanlı kimlik doğrulamayı yapılandırın
Varsayılan olarak, C SDK 3.9.2 ve sonraki sürümleri için SSL tabanlı kimlik doğrulama etkindir. SSL tabanlı kimlik doğrulama başarısız olursa SSL sertifikasının yolunun doğru olup olmadığını kontrol edin veya SSL tabanlı kimlik doğrulamayı devre dışı bırakın.
Aşağıdaki örnek kod, SSL tabanlı kimlik doğrulamanın nasıl yapılandırılacağını gösterir:
#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;
}