前置条件

初始化

点播服务的接入区域参考接入区域标识,例如:接入区域在上海,请使用cn-shanghai。

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

	// 点播服务接入区域
	regionId := "cn-shanghai"

	// 创建授权对象
	credential := &credentials.AccessKeyCredential{
		accessKeyId,
		accessKeySecret,
	}

	// 自定义config
	config := sdk.NewConfig()
	config.AutoRetry = true      // 失败是否自动重试
	config.MaxRetryTime = 3      // 最大重试次数
	config.Timeout = 3000000000  // 连接超时,单位:纳秒;默认为3秒

	// 创建vodClient实例
	return vod.NewClientWithOptions(regionId, config, credential)
}
			
  • 使用STS初始化
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) {

	// 点播服务接入区域
	regionId := "cn-shanghai"

	// 创建授权对象
	credential := &credentials.StsTokenCredential{
		accessKeyId,
		accessKeySecret,
		securityToken,
	}

	// 连接设置
	config := sdk.NewConfig()
	config.AutoRetry = true      // 失败是否自动重试
	config.MaxRetryTime = 3      // 最大重试次数
	config.Timeout = 3000000000  // 连接超时,单位:纳秒;默认为3秒

	// 创建vodClient实例
	return vod.NewClientWithOptions(regionId, config, credential)
}
			

使用说明

创建API请求时,调用的方法名为vod.Create${apiName}Request,其中${apiName}需要替换成要使用的点播API,可用的API名称参考点播API概览

调用示例

以调用获取播放地址接口GetPlayInfo为例:

func MyGetPlayInfo(client *vod.Client, videoId string) (response *vod.GetPlayInfoResponse, err error) {
	// 创建API请求并设置参数,调用vod.Create${apiName}Request
	request := vod.CreateGetPlayInfoRequest()
	request.VideoId = videoId
	request.AcceptFormat = "JSON"

	// 发起请求并处理异常,调用client.${apiName}(request)
	return client.GetPlayInfo(request)
}

func main() {
	client, err := InitVodClient("<AccessKeyId>", "<AccessKeySecret>")
	if err != nil {
		// 异常处理
		panic(err)
	}

	response, err := MyGetPlayInfo(client, "<videoId>")
	if err != nil {
		// 异常处理
		panic(err)
	}

	// fmt.Println(response)
	playList := response.PlayInfoList.PlayInfo
	for _, playInfo := range playList {
		// 打印清晰度和对应的播放地址
		fmt.Printf("%s: %s\n", playInfo.Definition, playInfo.PlayURL)
	}
}
			

附加说明