Initialization
Confirm Endpoint
Before confirming the Endpoint you configured when using the SDK, read the section of the Developer Guide on BBC access domain name section, to understand the concepts related to Endpoint. Baidu Cloud has opened support in multiple regions, please refer to description of region selection.
Get the Key
To use Baidu AI Cloud BBC, you need to have a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. AK/SK is assigned to users by the system and is a string to identify users and verify signatures for accessing BBC.
You can obtain and understand your AK/SK information through the following steps:
Create a BBC Client
BBC Client is the client of BBC service, which provides a series of methods for the developer to interact with BBC service.
Use AK/SK to create a new BBC Client
Access BBC via AK/SK, and users can create a Bbc Client by reference to the following codes:
import "github.com/baidubce/bce-sdk-go/services/bbc"
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>
// User specified Endpoint
ENDPOINT := <domain-name>
// Initialize a BBClient
bbcClient, err := bbc.NewClient(AK, SK, ENDPOINT)
}
In the above codes, ACCESS_KEY_ID corresponds to “Access Key ID” in the console, and SECRET_ACCESS_KEY corresponds to “Access Key Secret” in the console. Please refer to Get AKSK for the obtaining method. The third parameter “ENDPOINT” supports the domain name specified by the users themselves, and if it is set as a null string, default domain name is used as the service address of BBC.
Note: The
ENDPOINT
parameter needs to be defined with the domain name of the designated region. If the service region is Beijing, it isbbc.bj.baidubce.com
.
Configure HTTPS Protocol to Access BBC
BBC supports HTTPS, and you can use HTTPS to access BBC service in BBC GO SDK by indicating the mode of HTTPS in the Endpoint specified during creation of BBC Client object.
import "github.com/baidubce/bce-sdk-go/services/bbc"
ENDPOINT := "https://bbc.bj.baidubce.com" //Specify the use of HTTPS protocol
AK, SK := <your-access-key-id>, <your-secret-access-key>
bbcClient, _ := bbc.NewClient(AK, SK, ENDPOINT)
Set Network Parameters
Users can set the network parameters with the following sample code:
import "github.com/baidubce/bce-sdk-go/services/bbc"
AK, SK := <your-access-key-id>, <your-secret-access-key>
ENDPOINT := "bbc.bj.baidubce.com"
client, _ := bbc.NewClient(AK, SK, ENDPOINT)
// 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 Generation of Signature String Option
// import "github.com/baidubce/bce-sdk-go/services/bbc"
AK, SK := <your-access-key-id>, <your-secret-access-key>
ENDPOINT := "bbc.bj.baidubce.com"
client, _ := bbc.NewClient(AK, SK, ENDPOINT)
// Configure the HTTPS request header for signature as `Host`
headersToSign := map[string]struct{}{"Host": struct{}{}}
client.Config.SignOption.HeadersToSign = HeadersToSign
// Configure the valid period of signature as 30sec
client.Config.SignOption.ExpireSeconds = 30
Parameter Description
When users use GO SDK to access BBC, the Config
field of BBC Client object created supports all parameters in the table below:
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 |
Note:
1.The Credentials
field is created by the auth.NewBceCredentials
and auth.NewSessionBceCredentials
functions, and the former is used by default. The latter is used when STS authentication is used.
2.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 |
Among them, HeadersToSign defaults to Host
, Content-Type
, Content-Length
, Content-MD5
; TimeStamp is generally zero, indicating the timestamp when the authentication string is generated by the call, and the user should generally not clearly specify the value of this field; ExpireSeconds defaults to 1800 seconds or 30 minutes.
3.Retry
field specifies the retry policy, and support 2 retry policies currently: NoRetryPolicy
and BackOffRetryPolicy
. By default, the latter is used, and this retry policy 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.