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

Torch Object Storage

Copy page
Download PDF
.NET
Client Initialization (.NET SDK)
Copy page
Download PDF
Client Initialization (.NET SDK)
TosClient provides a series of interfaces for interacting with TOS services, enabling the management of resources such as buckets and objects within TOS. TosClient can be initialized using methods such as AccessKey/SecretKey, STS (Security Token Service), and others. During initialization, you may configure optional parameters such as connection establishment timeout and maximum number of idle connections. This article describes how to initialize the TOS .NET SDK.​
Prerequisites​
Configuring Long-Term Access Credentials​
Before initializing the SDK, you need to first configure long-term access credentials. This section outlines the procedures for configuring long-term access credentials on different operating systems.​
​
macOS
Linux
Windows
​
  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 document.​
​
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​
  • The Endpoint is the access domain name for TOS external services. For information on Endpoints supported by TOS, please refer to Access Domain Name Endpoint.​
​
using TOS;​
​
namespace ConsoleApp​
{​
internal class Program​
{​
private static void Main(string[] args)​
{​
var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");​
var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");​
var endpoint = "*** Provide your endpoint ***";​
var region = "*** Provide your region ***";​
​
var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).Build();​
}​
}​
}​
​
Use optional parameters to initialize​
The TOS .NET 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.​
​
using TOS;​
​
namespace ConsoleApp​
{​
internal class Program​
{​
private static void Main(string[] args)​
{​
var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");​
var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");​
var endpoint = "*** Provide your endpoint ***";​
var region = "*** Provide your region ***";​
​
var client = TosClientBuilder.Builder().​
SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).​
SetConnectionTimeout(30000).SetSocketTimeout(30000).​
Build();​
}​
}​
}​
​
Common Configuration Scenarios​
Configure Timeout Mechanism​
In the SDK, the timeout duration can be adjusted using optional parameters. The following functions are available to configure the timeout parameters:​
  • SetConnectionTimeout: Sets the connection timeout, which specifies the maximum waiting time during the connection establishment phase, measured in milliseconds. The default value is 10,000, equivalent to 10 seconds.​
  • SetSocketTimeout: Sets the timeout for reading and writing network streams, specifying the maximum wait time for a single read or write operation in milliseconds. The default value is 30000 (30 seconds).​
The following code demonstrates how to configure the SDK timeout.​
​
using TOS;​
​
namespace ConsoleApp​
{​
internal class Program​
{​
private static void Main(string[] args)​
{​
var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");​
var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");​
var endpoint = "*** Provide your endpoint ***";​
var region = "*** Provide your region ***";​
​
var client = TosClientBuilder.Builder().​
SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region).​
SetConnectionTimeout(10000).SetSocketTimeout(30000).​
Build();​
}​
}​
}​
​
Configuring the Connection Pool​
The TOS .NET SDK supports connection pool configuration. When initializing TosClient, you can add the MaxConnections parameter to specify the maximum number of connections in the pool.​
tip​
The default configuration for the connection pool size is 1024.​
The following code demonstrates how to configure the connection pool size for the SDK.​
​
using TOS;​
​
namespace ConsoleApp​
{​
internal class Program​
{​
private static void Main(string[] args)​
{​
var ak = Environment.GetEnvironmentVariable("TOS_ACCESS_KEY");​
var sk = Environment.GetEnvironmentVariable("TOS_SECRET_KEY");​
var endpoint = "*** Provide your endpoint ***";​
var region = "*** Provide your region ***";​
​
var client = TosClientBuilder.Builder().SetAk(ak).SetSk(sk).SetEndpoint(endpoint).SetRegion(region)​
.SetMaxConnections(1024).Build();​
}​
}​
}​
​
Initialization Parameter List​
​
Parameter Name​
​
​
Example value​
Description​
AK​
String​
Yes​
AKTPYmI1ZGQwMDA0NjlhNGFkMzhjNzM0N2Q0OTQ3ZTV****​
Access Key ID, that is, Key ID.​
SK​
String​
Yes​
T1dJM01UUXpOak0wTVdWak5E0poWldJNU1HWmxaV1V5TkdReVl6****​
SecretAccess Key, that is, the private access key.​
Endpoint​
String​
Optional; if left blank, 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​
The region where the TOS server is located.​
SecurityToken​
String​
No​
STSeyJBY2NvdW50SWQiOjIxMDAwMDUyMjQsIklkZW50aXR5VHlwZSI6NCwiSWRlbnRpdHlJZCI6MTE3MjI5NiwiQ2hhbm5lbCI6IlVzZXIiLCJBY2Nlc3NLZXlJZCI6IkFLVFBZbUkxWkdRd01mxoTkdGa016aGpOek0wTjJRME9UUTNaVFZqTXpFIiwiU2l****​
The security token within the temporary access credential.​
EnableVerifySSL​
Bool​
No​
true​
Configures whether SSL certificate verification is enabled; the default value is true.​
SocketTimeout​
Int​
No​
30000​
Socket read/write timeout duration, measured in milliseconds, with a default value of 30,000 ms (30 seconds).​
ConnectionTimeout​
Int​
No​
10000​
Set the connection establishment timeout in milliseconds. The default value is 10,000 ms (10 seconds).​
MaxConnections​
Int​
No​
1024​
Set the maximum number of connections for the connection pool; the default value is 1024.​
IdleConnectionTime​
Int​
No​
60*1000 (ms)​
Configure the maximum connection idle time; if this duration is exceeded, the connection will be closed. The default value is 60 seconds.​
​
​
Last updated: 2025.09.23 11:28:04