OSSClient serves as the OSS Java client to manage OSS resources such as buckets and objects.

To create an OSSClient instance, you need to specify an endpoint. For more information about endpoints, see Regions and endpoints and Bind a custom domain name.

The following table describes the parameters that you can configure:

Parameter Description Default value
UseCname Specifies whether the CNAME is used. No
Timeout Specifies the request timeout time in seconds, including the connection timeout and socket read-write timeout. 30 seconds for the connection timeout and 60 seconds for the read-write timeout
SecurityToken Specifies the securitytoken for temporary users Null
EnableMD5 Specifies whether the MD5 verification is enabled. CRC verification is recommended because it is more efficient than MD5 verification. No
EnableCRC Specifies whether CRC verification is enabled. Yes
Proxy Specifies the proxy server, such as http: // 8.8.8.8: 3128 Null
AuthProxy Specifies a proxy server that requires an account and a password to login. Null

Use an OSS domain to create an OSSClient instance

Run the following code to create an OSSClient instance with a domain assigned by OSS:

import "github.com/aliyun/aliyun-oss-go-sdk/oss"

client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err ! = nil {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

Use a custom domain (CNAME) to create an OSSClient instance

For the complete code of custome domain, see GitHub.

Run the following code to create an OSSClient instance with CNAME:

import "github.com/aliyun/aliyun-oss-go-sdk/oss"

// oss.UseCname (true) indicates thath the CNAME is enabled. CNAME indicates a custom domain bound to a bucket.
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>", oss.UseCname(true))
if err ! = nil {
	fmt.Println("Error:", err)
	os.Exit(-1)
}
Notice If the CNAME is enabled, you cannot list buckets because the CNAME is bound to a certain bucket.

Use STS to create an OSSClient instance

Run the following code to create an OSSClient instance with Security Token Service (STS):

import "github.com/aliyun/aliyun-oss-go-sdk/oss"

client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>", oss.SecurityToken("<yourSecurityToken>"))
if err ! = nil {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

For more information, see What is RAM and STS and Authorized access.

Set timeout

Run the following code to set connection timeout:

import "github.com/aliyun/aliyun-oss-go-sdk/oss"

// oss.Timeout(10, 120) indicates that the HTTP connection timeout is set to 10 seconds (the default value is 30 seconds) and the HTTP read-write timeout is set to 120 seconds (the default value is 60 seconds). The value 0 indicates that timeout never occurs, which is not recommend.
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>", oss.Timeout(10, 120))
if err ! = nil {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

CRC verification

CRC verification is enabled by default during uploads and downloads to ensure data integrity. Run the following code to disable CRC verification:

Warning We recommended that you do not disable CRC verification. If you disable CRC verification, the data integrity during uploads and downloads is not guaranteed.
import "github.com/aliyun/aliyun-oss-go-sdk/oss"

client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>", oss.EnableCRC(false))
if err ! = nil {
	fmt.Println("Error:", err)
	os.Exit(-1)
}