TosClient offers a series of interfaces to interact with TOS services, managing resources such as buckets and objects on TOS services. TosClient can be initialized using AccessKey/SecretKey, STS (Security Token Service), and other methods. During initialization, optional parameters such as connection timeout, retry count on timeout, and maximum idle connections can be set. This article explains how to initialize the TOS Python SDK.
Prerequisites
In single-process, multi-threaded scenarios, it is recommended to use a single TosClient to avoid repeated initialization of TosClient. In multi-process scenarios, multiple TosClients need to be created and cannot share the same TosClient, otherwise there will be multi-process concurrency security issues, which may lead to data corruption in read and write requests.
Configure long-term access credentials
Before initializing the SDK, configure long-term access credentials first. This section explains the steps to configure long-term access credentials on different operating systems.
- Open the terminal and execute the following command to open the file.
- Add the AKSK information at the end of the file.
- Execute the following command to apply the configuration.
- Execute the following command to verify the configuration.
If the configuration succeeds, the following example will be returned:
- Open the terminal and run the following command to open the file.
- Add AKSK information at the end of the file.
- Run the following command to apply the configuration.
- Run the following command to verify the configuration.
If the configuration is successful, the following example is returned:
- open the command line tool and run the following command to add AKSK information.
- run the following command to apply the configuration.
- restart the command line tool, then run the following command to verify the configuration.
if the configuration is successful, the following example will be returned:
Create TosClient
Initialize with required parameters
The following code initializes using the TOS domain name.
tip
- Endpoint is the publicly accessible domain name for TOS. For supported TOS Endpoint information, see Endpoint.
Use optional parameters
The following code initializes using optional parameters.
Common configuration scenarios
Configure STS
You can issue temporary access credentials to other users through the STS service. Within the credentials' validity period, users can access your TOS resources without exposing your long-term keys, ensuring the security of your TOS resources.
tip
You can obtain temporary access credentials via the AssumeRole interface of the STS service. These credentials include security keys (AccessKeyId, SecretAccessKey) and a security token (SessionToken). You can then use this information to access the TOS service. For basic operations on setting up the STS service, refer to using STS temporary AK/SK+Token to access Volcengine TOS. The following code initializes using STS.
Configure timeout mechanism
You can configure network request timeout when initializing TosClient by adding optional parameters. Currently, the TOS Python SDK supports configuring connection timeout connection_time and request timeout socket_timeout.
The following code configures timeout when initializing TosClient.
Configure retry policy
Network fluctuations or short-term TOS service exceptions (returning a 500 status code) can cause TOS service call failures. To address this, the Python SDK has an internal retry mechanism. Idempotent operation interfaces retry using an exponential backoff strategy after failure, while non-idempotent interfaces throw the corresponding exception directly. You can add optional parameters to configure the number of retries when initializing TosClient.
Configure data validation
When uploading or downloading objects, TOS returns the object's CRC value. The SDK verifies that the locally calculated CRC matches the CRC returned by TOS to ensure data integrity during upload and download. To ensure data integrity during uploads and downloads, the SDK enables CRC data validation by default. You can disable the CRC validation feature by adding optional parameters when initializing TosClient.
danger
It is strongly recommended not to disable CRC data validation. If this feature is disabled, TOS does not guarantee data integrity during uploads and downloads.
Configure proxy server
The TOS Python SDK supports accessing TOS services through a proxy server. You can configure the proxy server by adding optional parameters when initializing TosClient.
tip
Currently, only HTTP protocol proxy is supported.
Configure connection pool
TOS Python SDK supports connection pool configuration. You can add an optional parameter to set the pool size when initializing TosClient.
tip
The default connection pool size is 1024. It is recommended to use a single TosClient instance for single-process, multi-threaded scenarios.
Associating a custom domain
TOS Python SDK supports setting custom domains. You can pass a custom domain as the Host when initializing TosClient. After specifying a custom domain, you do not need to pass the Bucket in SDK interfaces.
Configure ECS password-free login
Passwordless login refers to authentication that does not require specifying a key, but instead uses temporary security credentials provided by the Security Token Service (STS) to verify identity and complete authorization. This method does not require revealing a long-term AccessKey, thus ensuring the security of keys under the account.
TOS Python SDK supports ECS password-free login. You can add the credentials_provider parameter when initializing TosClient.
tip
Before using ECS password-free login, first bind an IAM role to ECS. For details, see Binding IAM Role to ECS, and initialize TosClient using the role name as a parameter. Initialization parameter list
The following parameters are supported for initializing TosClient.