All Products
Search
Document Center

Initialization

Last Updated: Jul 11, 2019

Prerequisites

Initialization

The regionId parameter specifies the region of ApsaraVideo for VOD. Set this parameter to cn-shanghai for a region in China.

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) {

    // Sets the region of ApsaraVideo for VOD.
    regionId := "cn-shanghai"

    // Creates an authorization object.
    credential := &credentials.AccessKeyCredential{
        accessKeyId,
        accessKeySecret,
    }

    // Customizes the connection configuration.
    config := sdk.NewConfig()
    config.AutoRetry = true // Indicates whether the system automatically retries to connect to ApsaraVideo for VOD upon a connection failure.
    config.MaxRetryTime = 3 // The maximum number of retries allowed.
    config.Timeout = 3000000000 // The connection timeout duration, in nanoseconds. The default value 3000000000 indicates 3 seconds.

    // Creates a vodClient instance.
    return vod.NewClientWithOptions(regionId, config, credential)
}
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) {

    // Sets the region of ApsaraVideo for VOD.
    regionId := "cn-shanghai"

    // Creates an authorization object.
    credential := &credentials.StsTokenCredential{
        accessKeyId,
        accessKeySecret,
        securityToken,
    }

    // Customizes the connection configuration.
    config := sdk.NewConfig()
    config.AutoRetry = true // Indicates whether the system automatically retries to connect to ApsaraVideo for VOD upon a connection failure.
    config.MaxRetryTime = 3 // The maximum number of retries allowed.
    config.Timeout = 3000000000 // The connection timeout duration, in nanoseconds. The default value 3000000000 indicates 3 seconds.

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

Instructions

When calling the vod.Create${apiName}Request method to create an API request, you need to replace ${apiName} with the name of the ApsaraVideo for VOD operation to be called. For more information about available operations, see API overview.

Call example

The following code uses the GetPlayInfo operation as an example:

func MyGetPlayInfo(client *vod.Client, videoId string) (response *vod.GetPlayInfoResponse, err error) {
    // Calls the vod.Create${apiName}Request method to create an API request and sets parameters.
    request := vod.CreateGetPlayInfoRequest()
    request.VideoId = videoId
    request.AcceptFormat = "JSON"

    // Calls client.${apiName}(request) to initiate the request and handle exceptions (if any).
    return client.GetPlayInfo(request)
}

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

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

    // fmt.Println(response)
    playList := response.PlayInfoList.PlayInfo
    for _, playInfo := range playList {
        // The video definition and playback URL.
        fmt.Printf("%s: %s\n", playInfo.Definition, playInfo.PlayURL)
    }
}

Additional information