This topic describes how to initialize the server operation SDK for Go by using an AccessKey pair or a Security Token Service (STS) token. You can use one of the methods as needed.

Background information

The server SDK of ApsaraVideo VOD can be initialized by using two different methods. You can initialize the SDK by using the AccessKey pair of an Alibaba Cloud account or a RAM user that is granted required permissions based on authorization policies. The AccessKey pair remains valid after the Alibaba Cloud account or RAM user is created. We recommend that you use this method on the server. Alternatively, you can initialize the SDK by using an STS token that is granted required permissions based on authorization policies. You can specify the validity period of the STS token.

Prerequisites

  • The server operation SDK for Go is installed. For more information see Installation.
  • A region is specified for using ApsaraVideo VOD. For example, if you use ApsaraVideo VOD in the China (Shanghai) region, the region ID is cn-shanghai. For more information, see VOD centers and endpoints.

Initialize the SDK by using an AccessKey pair

Obtain an AccessKey pair to complete identity verification so that you can call server API operations. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.

Use the AccessKey pair to initialize the SDK. The following sample code provides an example:
package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
)

func InitVodClient(accessKeyId  string, accessKeySecret string) (client *vod.Client, err error) {

    // Specify the region from which you want to access ApsaraVideo VOD.
    regionId := "cn-shanghai"

    // Create an object for authentication.
    credential := &credentials.AccessKeyCredential{
        accessKeyId,
        accessKeySecret,
    }

    // The custom configuration parameters.
    config := sdk.NewConfig()
    config.AutoRetry = true      // Specifies whether to automatically reconnect to the network if a connection fails.
    config.MaxRetryTime = 3      // The maximum number of retry attempts.
    config.Timeout = 3000000000  // The connection timeout. Unit: nanoseconds. Default value: 3000000000.

    // Create a vodClient instance.
    return vod.NewClientWithOptions(regionId, config, credential)
}

Initialize the SDK by using an STS token

An STS token is obtained for initializing the SDK. For more information about how to obtain an STS token, see the "Use STS to authorize access" section of the Create a role and grant temporary access permissions to the role by using STS topic.

Use an STS token to initialize the SDK. The following sample code provides an example:
package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
)

func InitVodClient(accessKeyId  string, accessKeySecret string, securityToken string) (client *vod.Client, err error) {

    // Specify the region from which you want to access ApsaraVideo VOD.
    regionId := "cn-shanghai"

    // Create an object for authentication.
    credential := &credentials.StsTokenCredential{
        accessKeyId,
        accessKeySecret,
        securityToken,
    }

    // The configurations of the network connection.
    config := sdk.NewConfig()
    config.AutoRetry = true      // Specifies whether to automatically reconnect to the network if a connection fails.
    config.MaxRetryTime = 3      // The maximum number of retry attempts.
    config.Timeout = 3000000000  // The connection timeout. Unit: nanoseconds. Default value: 3000000000.

    // Create a vodClient instance.
    return vod.NewClientWithOptions(regionId, config, credential)
}

Usage notes

You must call vod.Create${apiName}Request to create an API request. ${apiName} indicates the ApsaraVideo VOD operation that you want to call. For more information about ApsaraVideo VOD operations, see List of operations by function.

Sample code

In this example, the GetPlayInfo operation is used to query a streaming URL. The following sample code provides an example:
func MyGetPlayInfo(client *vod.Client, videoId string) (response *vod.GetPlayInfoResponse, err error) {
    // Call vod.Create${apiName}Request to create an API request and configure the parameters.
    request := vod.CreateGetPlayInfoRequest()
    request.VideoId = videoId
    request.AcceptFormat = "JSON"

    // Call client.${apiName}(request) to send an API request and handle the exceptions.
    return client.GetPlayInfo(request)
}

func main() {
    client, err := InitVodClient("<AccessKeyId>", "<AccessKeySecret>")
    if err != nil {
        // Handle exceptions.
        panic(err)
    }

    response, err := MyGetPlayInfo(client, "<videoId>")
    if err != nil {
        // Handle exceptions.
        panic(err)
    }

    // fmt.Println(response)
    playList := response.PlayInfoList.PlayInfo
    for _, playInfo := range playList {
        // List the definition and streaming URL.
        fmt.Printf("%s: %s\n", playInfo.Definition, playInfo.PlayURL)
    }
}
Note
  • For more information, see Use Alibaba Cloud SDK for Go.
  • The API operations that are encapsulated in ApsaraVideo VOD SDK for Go support concurrent and generic calls.
  • You can view the sample code by downloading test_vod.go.