All Products
Search
Document Center

Object Storage Service:Sample code for timeout and retransmission using OSS Java SDK

Last Updated:Mar 20, 2026

Configure connection timeout and retry behavior in the OSS Java SDK using ClientConfiguration.

Parameters

ParameterMethodDescriptionDefault
connectionTimeoutsetConnectionTimeout(int)Timeout for establishing a connection, in milliseconds
maxErrorRetrysetMaxErrorRetry(int)Maximum number of retries after a request fails3

Sample code

The following example sets a 5,000-millisecond connection timeout and a maximum of 3 retries, then passes the configuration when creating the client.

ClientConfiguration conf = new ClientConfiguration();
// Set the connection timeout to 5,000 milliseconds.
conf.setConnectionTimeout(5000);
// Set the maximum number of retries to 3.
conf.setMaxErrorRetry(3);

OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);

Replace endpoint with your OSS endpoint URL, for example, https://oss-cn-hangzhou.aliyuncs.com.

The default value of maxErrorRetry is 3. Increase this value if your application needs to tolerate more transient network errors.