Initialization
Usage steps
Confirm Endpoint
Before using the SDK, you must confirm the endpoint (service domain) which Baidu AI Cloud will be integrated with. Read CCE endpoint to understand Endpoint concepts.
Create a Client object
Each specific service offers a client object, which provides a range of user-friendly methods for developers to easily interact with the respective service.
Call function API
Developers can use function APIs associated with the created client object of a specific service to take advantage of Baidu AI Cloud's features.
Example
Below is an example demonstrating basic usage.
1package main
2import (
3 "fmt"
4
5 cceapi "github.com/baidubce/bce-sdk-go/services/cce/v2"
6)
7func main() {
8 // User’s Access Key ID and Secret Access Key
9 accessKeyID, secretAccessKey := "<your-access-key-id>", "<your-secret-access-key>"
10 // CCE service endpoint
11 endpoint := "cce.bj.baidubce.com"
12 // Create CCE service client
13 cceClient, err := cceapi.NewClient(accessKeyID, secretAccessKey, endpoint)
14 // Get cluster list
15 args := &cceapi.ListClustersArgs{
16 KeywordType: "",
17 Keyword: "",
18 OrderBy: "",
19 Order: "",
20 PageSize: 10,
21 PageNum: 1,
22 }
23 result, err := cceClient.ListClusters(args)
24 if err != nil {
25 fmt.Println("list cluster failed:", err)
26 }
27 fmt.Println("requestID: ", result.RequestID)
28}
Configuration
Use HTTPS
The SDK supports accessing Baidu AI Cloud's service products through HTTPS. To use HTTPS, specify an HTTPS-based domain name in the endpoint when creating the client object for the service. The SDK will automatically detect and use HTTPS for access.
Detailed configurations
When developers use the GO SDK, the client object created for the corresponding service provides the following parameters in its exported Config field for detailed configurations:
| ConfigMap name | Types | Meaning |
|---|---|---|
| Endpoint | string | Domain name for service requests |
| ProxyUrl | string | The proxy address for client requests |
| Region | string | Region for resource requests |
| UserAgent | string | User name, HTTP request’s User-Agent header |
| Credentials | *auth.BceCredentials | Authentication object for requests, divided into regular AK/SK and STS |
| SignOption | *auth.SignOptions | Options for authentication string signing |
| Retry | RetryPolicy | Retry policy for connections |
| ConnectionTimeoutInMillis | int | Connection timeout, in milliseconds, defaulting to 20 minutes |
Developers can configure detailed parameters accordingly. Below are some configuration examples:
1// Client is the `Client` object of a specific service
2 // Configure request proxy address
3client.Config.ProxyUrl = "127.0.0.1:8080"
4 // Configure to not retry, default: Back Off retry
5client.Config.Retry = bce.NewNoRetryPolicy()
6 // Configure connection timeout to 30 seconds
7client.Config.ConnectionTimeoutInMillis = 30 * 1000
8 // Configure the HTTP request header Host for signing
9client.Config.SignOption.HeadersToSign = map[string]struct{}{"Host": struct{}{}}
10 // Configure the validity period of the signature to 30 seconds
11client.Config.SignOption.ExpireSeconds = 30
Description:
- The Credentials field is created using the auth.NewBceCredentials and auth.NewSessionBceCredentials functions. The default function is the former, while the latter is used for STS authentication scenarios.
- The SignOption field represents options when generating a signature string, as detailed in the table below:
| Name | Types | Meaning |
|---|---|---|
| HeadersToSign | map[string]struct{} | HTTP headers used when generating the signature string |
| Timestamp | int64 | Timestamp used in the generated signature string, defaulting to the value at the time of sending request |
| ExpireSeconds | int | Validity period of the signature string |
Among the configuration options, HeadersToSign defaults to Host, Content-Type, Content-Length, and Content-MD5. TimeStamp is usually set to zero, which means the timestamp when the signature string is generated (in seconds) will be used, and users normally do not need to specify a value for this field. ExpireSeconds defaults to 1,800 seconds (30 minutes).
- The Retry field defines the retry strategy, currently supporting two types: NoRetryPolicy and BackOffRetryPolicy. By default, the BackOffRetryPolicy is applied. This policy determines the maximum retry attempts, the maximum retry duration, and the retry base. Retry intervals grow exponentially, calculated as the retry base multiplied by 2, until either the maximum number of retries or the maximum duration limit is reached.
