更新时间:2024-01-31 GMT+08:00

Go SDK

本章节介绍了Go SDK的使用说明,您可以参考本章节进行快速集成开发。

开发前准备

  • 注册华为帐号并开通华为云,已进行实名认证

    国际站用户在以下情况下需要进行帐号实名认证。

    • 根据中国大陆相关法规要求,购买和使用中国大陆节点云产品服务的用户需要实名认证。
    • 购买媒体处理服务时,如果您选择的区域包含中国大陆,则需要实名认证。
  • 具体开发环境 ,支持go 1.14及以上版本。
  • 已获取帐号对应的Access Key(AK)和Secret Access Key(SK)。请在控制台我的凭证 > 访问密钥页面上创建和查看您的AK/SK。具体请参见访问密钥
  • 已获取转码服务对应区域的项目ID,请在控制台我的凭证 > API凭证页面上查看项目ID。具体请参见API凭证
  • 已将需要处理的媒资文件上传至MPC同区域的OBS桶中,并将OBS桶进行授权,允许MPC访问。具体请参见上传音视频文件获取云资源授权

安装SDK

媒体转码Go SDK支持go 1.14及以上版本。执行go version检查当前Go的版本信息。

使用go get安装Go SDK,执行如下命令安装Go SDK库以及相关依赖库,具体的SDK版本号请参见SDK开发中心

1
2
3
4
# 安装Go库
go get github.com/huaweicloud/huaweicloud-sdk-go-v3
# 安装依赖
go get github.com/json-iterator/go

开始使用

  1. 导入依赖模块。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    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"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mpc/v1"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mpc/v1/model"
        "net/http"
    )
    

  2. 配置客户端属性。

    1. 默认配置
      1
      2
      # Use default configuration
      httpConfig := config.DefaultHttpConfig()
      
    2. 代理配置(可选)。
      1
      2
      3
      4
      5
      6
      7
      8
      9
      username := os.Getenv("USER_NAME")
      password := os.Getenv("USER_PASSWARD")
      // 根据需要配置网络代理
      httpConfig.WithProxy(config.NewProxy().
          WithSchema("http").
          WithHost("proxy.huaweicloud.com").
          WithPort(80).
          WithUsername(username).
          WithPassword(password))
      
    3. SSL配置(可选)
      1
      2
      // 根据需要配置是否跳过SSL证书校验
      httpConfig.WithIgnoreSSLVerification(true);
      

  3. 初始化认证信息。

    支持两种方式认证,您可以根据实际情况进行选择。

    • 使用永久AK/SK

      首先需要获取永久AK和SK,以及projectId,您可以参考开发前准备获取。

      1
      2
      3
      4
      5
      6
      7
      8
      ak := os.Getenv("SDK_AK")
      sk := os.Getenv("SDK_SK")
      projectId := os.Getenv("PROJECT_ID")
      auth := basic.NewCredentialsBuilder().
                  WithAk(ak).
                  WithSk(sk).
                  WithProjectId(projectId).
                  Build()
      
    • 使用临时AK/SK

      首先需要获得临时AK、SK和SecurityToken,您可以通过token获取或者通过委托授权获取

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      ak := os.Getenv("SDK_AK")
      sk := os.Getenv("SDK_SK")
      projectId := os.Getenv("PROJECT_ID")
      securityToken := os.Getenv("SECURITY_TOKEN")
      auth := basic.NewCredentialsBuilder().
                  WithAk(ak).
                  WithSk(sk).
                  WithProjectId(projectId).
                  WithSecurityToken(securityToken).
                  Build()
      
    相关参数说明如下所示:
    • ak:帐号Access Key。
    • sk:帐号Secret Access Key 。
    • projectId:云服务所在项目ID ,根据你想操作的项目所属区域选择对应的项目ID。
    • securityToken:采用临时AK/SK认证场景下的安全票据。

  4. 初始化客户端。

    1
    2
    3
    4
    5
    6
    7
    8
    # 初始化MPC的客户端
    client := mpc.NewMpcClient
    (
        mpcMpcClientBuilder().
            WithEndpoint(endpoint).     // endpoint值如 "https://mpc.region01.myhuaweicloud.com"
            WithCredential(auth).
            WithHttpConfig(config.DefaultHttpConfig()).  
            Build())
    

    endpoint:MPC应用区域和各服务的终端节点,具体请参见地区和终端节点

  5. 发送请求并查看响应。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    // 初始化请求,以调用接口查询转码模板为例
    request := &model.ListTranscodingTaskRequest{    
        TaskId:&[]int64{1900293},
    }
    response, err := client.ListTranscodingTask(request)
    if err == nil {
        fmt.Printf("%+v\n",response)
    } else {
        fmt.Println(err)
    }
    

  6. 异常处理。

    表1 异常处理

    一级分类

    一级分类说明

    ServiceResponseError

    service response error

    url.Error

    connect endpoint error

    1
    2
    3
    4
    5
    6
    7
    # 异常处理
    response, err := client.ListTranscodingTask(request)
    if err == nil {
        fmt.Println(response)
    } else {
        fmt.Println(err)
    }
    

  7. 原始Http侦听器。

    在某些场景下可能对业务发出的Http请求进行Debug,需要看到原始的Http请求和返回信息,SDK提供侦听器功能来获取原始的为加密的Http请求和返回信息。

    原始信息打印仅在debug阶段使用,请不要在生产系统中将原始的Http头和Body信息打印到日志,这些信息并未加密且其中包含敏感数据;当Body体为二进制内容,即Content-Type标识为二进制时 body为"***",详细内容不输出。

     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
    func RequestHandler(request http.Request) {
        fmt.Println(request)
    }
    
    func ResponseHandler(response http.Response) {
        fmt.Println(response)
    }
    
    ak := os.Getenv("SDK_AK")
    sk := os.Getenv("SDK_SK")
    projectId := os.Getenv("{your project id}")
    client := mpc.NewMpcClient(
        mpc.MpcClientBuilder().
            WithEndpoint("{your endpoint}").
            WithCredential(
                basic.NewCredentialsBuilder().
                    WithAk(ak).
                    WithSk(sk).
                    WithProjectId(projectId).
                       Build()).
            WithHttpConfig(config.DefaultHttpConfig().
                WithIgnoreSSLVerification(true).
                WithHttpHandler(httphandler.
                    NewHttpHandler().
                        AddRequestHandler(RequestHandler).
                        AddResponseHandler(ResponseHandler))).Build())
    

代码示例

调用前请根据实际情况替换如下变量:"SDK_AK"、"SDK_SK"、{your endpoint string}以及{your project id}。
 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
50
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"
    mpc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mpc/v1"
    "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mpc/v1/model"
    "net/http"
)

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

func ResponseHandler(response http.Response) {
    fmt.Println(response)
}
ak := os.Getenv("SDK_AK")
sk := os.Getenv("SDK_SK")
projectId := os.Getenv("{your project id}")
func main() {
    client := mpc.NewMpcClient(
        mpc.MpcClientBuilder().
            WithEndpoint("{your endpoint}").
            WithCredential(
                basic.NewCredentialsBuilder().
                    WithAk(ak).
                    WithSk(sk).
                    WithProjectId(projectId).
                    Build()).
            WithHttpConfig(config.DefaultHttpConfig().
                WithIgnoreSSLVerification(true).
                WithHttpHandler(httphandler.
                    NewHttpHandler().
                        AddRequestHandler(RequestHandler).
                        AddResponseHandler(ResponseHandler))).
            Build())

    request := &model.ListTranscodingTaskRequest{        
        TaskId:&[]int64{1900293},
    }
    response, err := client.ListTranscodingTask(request)
    if err == nil {
        fmt.Println("%+v\n",response)
    } else {
        fmt.Println(err)
    }
}