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.
- Open the terminal and execute the following command to open the file.
- Add AKSK information at the end of the document.
export TOS_ACCESS_KEY=AKTPYmI1Z****
export TOS_SECRET_KEY=T1dJM01UU****
- Execute the following command to make the configuration information take effect.
- Execute the following command to verify the configuration information.
If the configuration is successful, the following example is returned:
- Open the terminal and execute the following command to open the file.
- Add AKSK information at the end of the document.
export TOS_ACCESS_KEY=AKTPYmI1Z****
export TOS_SECRET_KEY=T1dJM01UU****
- Execute the following commands for the configuration information to take effect.
- Execute the following command to verify the configuration information.
If the configuration is successful, the following example will be returned:
- Open the command-line tool, execute the following command, and add the AKSK information.
set TOS_ACCESS_KEY=AKTPYmI1Z****
set TOS_SECRET_KEY=T1dJM01UU****
- Execute the following command for the configuration to take effect.
setx TOS_ACCESS_KEY "%TOS_ACCESS_KEY%"
setx TOS_SECRET_KEY "%TOS_SECRET_KEY%"
- Restart the command-line tool, and then execute the following command to verify the configuration information.
If the configuration is successful, the following example is returned:
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.
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.
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).
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.
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).
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.
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