Initialization
How to Use It
Confirm Endpoint
Before using the SDK, you need to confirm the Endpoint (service domain name) of the Baidu AI Cloud product you will access. Taking Baidu's private network products as an example, you can read the part of VPC access domain name to understand the concepts related to Endpoint. Other services are similar, you need to understand and confirm the Endpoint of the corresponding service.
Create Client object
Each specific service has a Client
object, which encapsulates a series of easy-to-use methods for developers to interact with the corresponding service. Developers can refer to the documentation in the directory corresponding to the specific service in the SDK to use the corresponding service.
Call feature interface
Based on the created Client
object of the corresponding service, the developer can call the corresponding functional interface and use the features of Baidu AI Cloud products.
Example
The following takes Baidu AI Cloud Virtual Private Cloud (VPC) as an example to give a basic usage example. For detailed usage instructions, please refer to the detailed documentation of each service.
import (
"github.com/baidubce/bce-sdk-go/services/vpc"
)
func main() {
// Access Key ID and Secret Access Key of users
ACCESS_KEY_ID, SECRET_ACCESS_KEY := "<your-access-key-id>", "<your-secret-access-key>"
// Endpoint of VPC service
ENDPOINT := "<domain-name>"
// Create Client for VPC Service
vpcClient, err := vpc.NewClient(AK, SK, ENDPOINT)
// Create a VPC
args := &CreateVPCArgs{
Name: "TestSDK-VPC",
Description: "vpc test",
Cidr: "192.168.0.0/16",
ClientToken: "clientToken",
}
result, err := vpcClient.CreateVPC("<your-create-vpc-args>"); err != nil {
if err != nil {
fmt.Println("create vpc failed:", err)
}
fmt.Println("create vpc success ,vpc id ", result.VPCID)
}
Configure
Use HTTPS protocol
The SDK supports the use of HTTPS protocol to access Baidu AI Cloud's service products. To use the HTTPS protocol, you only need to specify the domain name using the https protocol in the Endpoint specified when you create the Client
object of the corresponding service. SDK will automatically recognize and use the HTTPS protocol to access.
Detailed configuration
When the developer uses the GO SDK, the Client
object of the corresponding service created, and its export field Config
provides the following parameters to support detailed configuration:
Name of configuration item | Type | Meaning |
---|---|---|
Endpoint | string | Domain name of request service |
ProxyUrl | string | Agent address of client request |
Region | string | Region of request resources |
UserAgent | string | User name, User-Agent header of HTTP request |
Credentials | *auth.BceCredentials | Authentication object of request is classified into ordinary AK/SK and STS |
SignOption | *auth.SignOptions | Signature option of authentication string |
Retry | RetryPolicy | Connection retry policy |
ConnectionTimeoutInMillis | int | Connection time-out period, in msec, and the default is 20min |
Description:
Credentials
field is created withauth.NewBceCredentials
andauth.NewSessionBceCredentials
functions, with the former as the default, and the latter used for STS authentication.SignOption
field is the option to generate signature string, for more information, please see the explanation in the table below:
Name | Type | Meaning |
---|---|---|
HeadersToSign | map[string]struct{} | HTTP header used to generate signature string |
Timestamp | int64 | The time stamp used to generate signature string is the value when the request is sent by default. |
ExpireSeconds | int | Valid period of signature string |
Where the default values of HeadersToSign are `Host`, `Content-Type`, `Content-Length` and `Content- MD5`; TimeStamp is generally zero, indicating the time stamp when call is used to generate string, and generally the value of that field should not be specified expressly; the default value of ExpireSeconds is 1,800sec, i.e. 30min.
Retry
field specifies the retry strategy, and support 2 retry strategies currently:NoRetryPolicy
andBackOffRetryPolicy
. By default, the latter is used, and this retry strategy specifies the maximum number of retry, longest time of retry and retry base. Retry is performed by multiplying the retry base by exponential growth of 2 until the maximum retry test or the maximum retry time is reached.
Developers can configure detailed parameters accordingly. Some configuration examples are given below:
// Client is a `Client` object for a specific service
// Configure request proxy address
client.Config.ProxyUrl = "127.0.0.1:8080"
// Configuration is not retried, and the default is Back Off retry
client.Config.Retry = bce.NewNoRetryPolicy()
// Configure the connection time-out period as 30sec
client.Config.ConnectionTimeoutInMillis = 30 * 1000
// Configure the HTTPS request header for signature as `Host`
client.Config.SignOption.HeadersToSign = map[string]struct{}{"Host": struct{}{}}
// Configure the valid period of signature as 30sec
client.Config.SignOption.ExpireSeconds = 30