You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
C
Initializing the Client (C SDK)
Copy page
Download PDF
Initializing the Client (C SDK)
TosClient provides a series of interfaces for interacting with the TOS service, enabling management of resources such as buckets and objects on the TOS service. TosClient can be initialized using methods such as AccessKey/SecretKey, STS (Security Token Service), etc. During initialization, you can configure optional parameters such as the connection establishment timeout and the maximum number of idle connections. This article describes how to initialize the TOS C SDK.​
Prerequisites​
Configure Long-Term Access Credential​
Before initializing the SDK, you must first configure long-term access credentials. This chapter describes the steps for configuring long-term access credentials on different operating systems.​
tip​
The use of the C SDK on Windows systems is currently not supported.​
​
macOS
Linux
​
  1. Open the terminal and execute the following command to open the file.​
​
nano ~/.bash_profile​
​
  1. Add AKSK information at the end of the file.​
​
export TOS_ACCESS_KEY=AKTPYmI1Z****​
export TOS_SECRET_KEY=T1dJM01UU****​
​
  1. Save the file and exit.​
  1. Execute the following command to make the configuration information take effect.​
​
source ~/.bash_profile​
​
  1. Execute the following command to verify the configuration information.​
​
echo $TOS_ACCESS_KEY​
echo $TOS_SECRET_KEY​
​
If the configuration is successful, the following example is returned:​
​
AKTPYmI1Z****​
T1dJM01UU****​
​
​
Create TosClient​
Initialize with required parameters​
The following code demonstrates how to initialize TosClient using required parameters such as the TOS domain name, AK, SK, Endpoint, and Region.​
tip​
  • Endpoint is the access domain name for TOS external services. For information about endpoints supported by TOS, see Access Domain Endpoint.​
​
char* ak = getenv("TOS_ACCESS_KEY");​
char* sk = getenv("TOS_SECRET_KEY");​
​
void init_client_demo()​
{​
​
tos_pool_t *pool;​
tos_client_t *client;​
​
​
tos_pool_create(&pool, NULL);​
​
​
​
client = tos_client_create(pool);​
client->config = tos_config_create(client->pool);​
​
​
​
tos_str_set(&client->config->endpoint, "*** Provide your endpoint ***");​
tos_str_set(&client->config->region, "*** Provide your region ***");​
tos_str_set(&client->config->access_key_id, ak);​
tos_str_set(&client->config->access_key_secret, sk);​
​
tos_pool_destroy(pool);​
}​
​
Initialize Using Optional Parameters​
The TOS C SDK provides multiple optional parameters to customize the configuration of TosClient. The following code demonstrates how to customize the connection establishment timeout and read/write timeout settings for TosClient.​
​
char* ak = getenv("TOS_ACCESS_KEY");​
char* sk = getenv("TOS_SECRET_KEY");​
​
void init_client_demo()​
{​
​
tos_pool_t *pool;​
tos_client_t *client;​
​
​
tos_pool_create(&pool, NULL);​
​
​
​
client = tos_client_create(pool);​
client->config = tos_config_create(client->pool);​
​
​
​
tos_str_set(&client->config->endpoint, "*** Provide your endpoint ***");​
tos_str_set(&client->config->region, "*** Provide your region ***");​
tos_str_set(&client->config->access_key_id, ak);​
tos_str_set(&client->config->access_key_secret, sk);​
​
​
​
​
client->ctl->options->connect_timeout = 5; ​
client->ctl->options->socket_timeout = 60; ​
tos_pool_destroy(pool);​
}​
​
Common Configuration Scenarios​
Configure Timeout Mechanism​
In the SDK, the timeout duration can be adjusted using optional parameters, and the timeout parameters can be configured through the following functions:​
  • connect_timeout: Specifies the connection timeout, which is the maximum wait time during the connection establishment phase, measured in seconds. The default value is 10, meaning 10 seconds.​
  • SetSocketTimeout: Sets the timeout for reading and writing network streams, specifying the maximum wait time for a single Read/Write operation in seconds. The default value is 120, that is, 120 seconds.​
The following code demonstrates how to set the SDK timeout.​
​
char* ak = getenv("TOS_ACCESS_KEY");​
char* sk = getenv("TOS_SECRET_KEY");​
​
void init_client_demo()​
{​
​
tos_pool_t *pool;​
tos_client_t *client;​
​
​
tos_pool_create(&pool, NULL);​
​
​
​
​
client = tos_client_create(pool);​
client->config = tos_config_create(client->pool);​
​
​
​
tos_str_set(&client->config->endpoint, "*** Provide your endpoint ***");​
tos_str_set(&client->config->region, "*** Provide your region ***");​
tos_str_set(&client->config->access_key_id, ak);​
tos_str_set(&client->config->access_key_secret, sk);​
​
​
​
​
client->ctl->options->connect_timeout = 5; ​
client->ctl->options->socket_timeout = 60; ​
tos_pool_destroy(pool);​
}​
​
Initialization Parameter List​
​
Parameter Name​
Parameter Type​
Is it mandatory​
Example Value​
Description​
access_key_id​
String​
Required​
AKTPYmI1ZGQwMDA0NjlhNGFkMzhjNzM0N2Q0OTQ3ZTV****​
Access Key ID (key ID).​
access_key_secret​
String​
Required​
T1dJM01UUXpOak0wTVdWak5E0poWldJNU1HWmxaV1V5TkdReVl6****​
SecretAccess Key, i.e., the private access key.​
endpoint​
String​
Optional; if left empty, the value is determined by the Region parameter.​
tos-cn-beijing.volces.com​
TOS server-side access domain name.​
region​
String​
Optional; required if Endpoint is empty​
cn-beijing​
Region where the TOS server is located.​
sts_token​
String​
Optional​
STSeyJBY2NvdW50SWQiOjIxMDAwMDUyMjQsIklkZW50aXR5VHlwZSI6NCwiSWRlbnRpdHlJZCI6MTE3MjI5NiwiQ2hhbm5lbCI6IlVzZXIiLCJBY2Nlc3NLZXlJZCI6IkFLVFBZbUkxWkdRd01mxoTkdGa016aGpOek0wTjJRME9UUTNaVFZqTXpFIiwiU2l****​
The security token in the temporary access credential.​
socket_timeout​
Int​
Optional​
120​
Socket read/write timeout period, measured in milliseconds, with a default value of 30000 ms (i.e., 30 s).​
connect_timeout​
Int​
Optional​
10​
Set the connection establishment timeout in milliseconds. The default value is 10000 ms, which is equivalent to 10 seconds.​
​
​
Last updated: 2025.09.26 19:05:16