Updated on 2024-02-27 GMT+08:00

Go SDK User Guide

This section describes how to quickly integrate Go SDKs for development.

Prerequisites

  • You have registered a Huawei account.

    If you are a Huawei Cloud (International) user, you need to complete real-name authentication when you:

    • Purchase and use cloud services on Huawei Cloud nodes in the Chinese mainland. In this case, real-name authentication is required by the laws and regulations of the Chinese mainland.
    • Select the Chinese mainland region for Live.
  • You have obtained licensed domain names for streaming and playback, added an ingest domain name and a streaming domain name on the Live console, and associated domain names.
  • The development environment (Go 1.14 or later) is available.
  • You have obtained the access key ID (AK) and secret access key (SK) of the Huawei Cloud account. You can create and view your AK/SK on the My Credentials > Access Keys page of the Huawei Cloud console. For details, see Access Keys.
  • You have obtained the project ID of the corresponding region of Live. You can view the project ID on the My Credentials > API Credentials page of the Huawei Cloud console. For details, see API Credentials.

Installing an SDK

The Live SDK supports Go 1.14 or later. Run the go version command to check the Go version.

Run the go get command to install the Huawei Cloud Go SDK. Then run the following commands to install the Huawei Cloud Go SDK library and dependencies. For details about the SDK version, see SDK Center.

1
2
3
4
# Install the Huawei Cloud Go library.
go get github.com/huaweicloud/huaweicloud-sdk-go-v3
# Install dependencies.
go get github.com/json-iterator/go

Procedure

  1. Import the dependent module.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    import (
        "fmt"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/httphandler"
        live "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/live/v1"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/live/v1/model"
        "net/http"
        "os"
    )
    

  2. Configure client attributes.

    1. Use the default configuration.
      1
      2
      # Use default configuration
      httpConfig := config.DefaultHttpConfig()
      
    2. (Optional) Configure a proxy.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      // Configure the network proxy as required.
      // There will be huge security risks if the password of the proxy server is directly written into the code. You are advised to store the password in ciphertext in the configuration file or environment variables and decrypt the password when using it.
      // Before configuring a proxy, specify the environment variable PROXY_PASSWORD in the local environment.
      httpConfig.WithProxy(config.NewProxy().
          WithSchema("http").
          WithHost("proxy.huaweicloud.com").
          WithPort(80).
          WithUsername("testuser").
          WithPassword(os.Getenv("PROXY_PASSWORD")))
      
    3. (Optional) Configure a connection.
      1
      httpConfig.WithTimeout(30);
      
    4. (Optional) Configure SSL.
      1
      2
      // Configure whether to skip SSL certificate verification as required.
      httpConfig.WithIgnoreSSLVerification(true);
      

  3. Initialize authentication information.

    You can use one of the following two authentication modes:

    The related parameters are as follows:
    • ak: AK of the Huawei Cloud account. You are advised to store the AK in ciphertext in the configuration file or environment variables and decrypt it when using it.
    • sk: SK of the Huawei Cloud account. You are advised to store the SK in ciphertext in the configuration file or environment variables and decrypt it when using it.
    • projectId: ID of the project where Live is provided. Select a project ID based on the region of the project.
    • securityToken: security token used for temporary AK/SK authentication

  4. Initialize the client.

    1
    2
    3
    4
    5
    6
    7
    # Initialize the Live client.
    client := live.NewLiveClient(
        live.LiveClientBuilder().
            WithEndpoints(endpoints).
            WithCredential(auth).
            WithHttpConfig(config.DefaultHttpConfig()).  
            Build())
    

    endpoint: regions where Live is used and endpoints of each service. For details, see Regions and Endpoints.

  5. Send a request and view the response.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    // Initialize the request. The following uses the API for querying transcoding templates as an example.
    request := &model.ShowTranscodingsTemplateRequest{
        Domain: "play.example.huaweicloud.com",
    }
    response, err := client.ShowTranscodingsTemplate(request)
    if err == nil {
        fmt.Printf("%+v\n",response)
    } else {
        fmt.Println(err)
    }
    

  6. Perform troubleshooting.

    Table 1 Troubleshooting

    Level 1

    Description

    ServiceResponseError

    Service response error

    url.Error

    Endpoint connection error

    1
    2
    3
    4
    5
    6
    7
    # Troubleshooting
    response, err := client.ShowTranscodingsTemplate(request)
    if err == nil {
        fmt.Println(response)
    } else {
        fmt.Println(err)
    }
    

  7. Use the listener to obtain original HTTP requests and responses.

    The original HTTP requests and responses are required for debugging HTTP requests sent by the service side. The SDK provides the listener to obtain the original and encrypted HTTP requests and responses.

    Original information is printed only during debugging. Do not print the header and body of an original HTTP request in the production system because this information contains sensitive data but is not encrypted. If the request body is binary, that is, Content-Type is set to binary, the body will be displayed as *** without the detailed content.

    There will be huge security risks if the AK and SK used for authentication are directly written into the code. You are advised to store the AK and SK in ciphertext in the configuration file or environment variables and decrypt them when using them.

    In this example, the AK and SK stored in the environment variables are used. Specify the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    func RequestHandler(request http.Request) {
        fmt.Println(request)
    }
    
    func ResponseHandler(response http.Response) {
        fmt.Println(response)
    }
    
    client := live.NewLiveAPIClient(
        live.LiveAPIClientBuilder().
            WithEndpoints([]string{"{your endpoint}"}).
            WithCredential(
                basic.NewCredentialsBuilder().
                    WithAk(os.Getenv("HUAWEICLOUD_SDK_AK")).
                    WithSk(os.Getenv("HUAWEICLOUD_SDK_SK")).
                    WithProjectId("{your project id}").
                       Build()).
            WithHttpConfig(config.DefaultHttpConfig().
                WithIgnoreSSLVerification(true).
                WithHttpHandler(httphandler.
                    NewHttpHandler().
                        AddRequestHandler(RequestHandler).
                        AddResponseHandler(ResponseHandler))).
            Build())
    

Sample Code

Before the calling, replace the variables {your endpoint} and {your project id} as needed.

There will be huge security risks if the AK and SK used for authentication are directly written into the code. You are advised to store the AK and SK in ciphertext in the configuration file or environment variables and decrypt them when using them.

In this example, the AK and SK are stored in environment variables. Before running this example, specify the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main

import (
    "fmt"
    "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
    "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/httphandler"
    live "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/live/v1"
    "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/live/v1/model"
    "net/http"
    "os"
)

func RequestHandler(request http.Request) {
    fmt.Println(request)
}

func ResponseHandler(response http.Response) {
    fmt.Println(response)
}

func main() {
    client := live.NewLiveAPIClient(
        live.LiveAPIClientBuilder().
            WithEndpoints([]string{"{your endpoint}"}).
            WithCredential(
                basic.NewCredentialsBuilder().
                    WithAk(os.Getenv("HUAWEICLOUD_SDK_AK")).
                    WithSk(os.Getenv("HUAWEICLOUD_SDK_SK")).
                    WithProjectId("{your project id}").
                    Build()).
            WithHttpConfig(config.DefaultHttpConfig().
                WithIgnoreSSLVerification(true).
                WithHttpHandler(httphandler.
                    NewHttpHandler().
                        AddRequestHandler(RequestHandler).
                        AddResponseHandler(ResponseHandler))).
            Build())

    request := &model.ShowTranscodingsTemplateRequest{
        Domain: "play.example.huaweicloud.com",
    }
    response, err := client.ShowTranscodingsTemplate(request)
    if err == nil {
        fmt.Println("%+v\n",response)
    } else {
        fmt.Println(err)
    }
}