Initialization
Usage steps
Confirm Endpoint
Before using the SDK, you must confirm the endpoint (service domain) which Baidu AI Cloud will be integrated with. Taking Baidu's Virtual Private Cloud (VPC) product as an example, you can read the Service Domain Names section to understand concepts related to endpoints. Similar to other services, it is necessary to understand and confirm the endpoints of the corresponding services.
Create a 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 directory corresponding to the specific service within the SDK to use the relevant service.
Call function API
Developers can call corresponding function APIs based on the created Client object of the relevant service to utilize the functions of Baidu AI Cloud.
Example
Using Baidu AI Cloud’s Virtual Private Cloud (VPC) as an example, a basic usage demonstration is provided below. For detailed usage instructions, refer to the service-specific documentation.
1import (
2 "github.com/baidubce/bce-sdk-go/services/vpc"
3)
4func main() {
5 // User’s Access Key ID and Secret Access Key
6 ACCESS_KEY_ID, SECRET_ACCESS_KEY := "<your-access-key-id>", "<your-secret-access-key>"
7 // VPC service endpoint
8 ENDPOINT := "<domain-name>"
9 // Create VPC service client
10 vpcClient, err := vpc.NewClient(AK, SK, ENDPOINT)
11 // Create VPC
12 args := &CreateVPCArgs{
13 Name: "TestSDK-VPC",
14 Description: "vpc test",
15 Cidr: "192.168.0.0/16",
16 ClientToken: "clientToken",
17 }
18 result, err := vpcClient.CreateVPC("<your-create-vpc-args>"); err != nil {
19 if err != nil {
20 fmt.Println("create vpc failed:", err)
21 }
22 fmt.Println("create vpc success ,vpc id ", result.VPCID)
23}
Configuration
Use HTTPS
The SDK supports access to the service products of Baidu AI Cloud by HTTPS. To use HTTPS, you can simply specify the domain name using HTTPS in the endpoint when creating the Client object corresponding to the service. The SDK will automatically recognize 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 |
Description:
- The
Credentials fieldis created using theauth.NewBceCredentialsandauth.NewSessionBceCredentialsfunctions. The former is used by default, while the latter is used for STS authentication. - The
SignOptionfield 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 |
1 Among configuration options, HeadersToSign defaults to `Host`, `Content-Type`, `Content-Length` and `Content-MD5`; TimeStamp is typically set to zero, indicating that the timestamp at the time of generating the certification string shall be used, and users generally shall not explicitly specify the value for this field; ExpireSeconds defaults to 1,800 seconds or 30 minutes.
- The
Retryfield specifies the retry policy, currently supporting two types:NoRetryPolicyandBackOffRetryPolicy. By default, the latter is used. This retry policy specifies the maximum number of retries, the maximum retry duration, and the retry base. Retries increase exponentially based on the retry base multiplied by 2 until the maximum number of retries or the maximum retry duration is reached.
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
