All Products
Search
Document Center

Initialization

Last Updated: May 27, 2020

Prerequisites

  • You have registered an Alibaba Cloud account, completed real-name verification, and activated ApsaraVideo for VOD.
  • You have obtained an AccessKey for access to ApsaraVideo for VOD. You can create an AccessKey for your Alibaba Cloud account on the AccessKey management page of the Alibaba Cloud console. Alternatively, you can create a RAM user in the RAM console and grant the user the permission to access ApsaraVideo for VOD. For more information, see RAM user access.

Initialization

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

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. )
  8. func InitVodClient(accessKeyId string, accessKeySecret string) (client *vod.Client, err error) {
  9. // Sets the region of ApsaraVideo for VOD.
  10. regionId := "cn-shanghai"
  11. // Creates an authorization object.
  12. credential := &credentials.AccessKeyCredential{
  13. accessKeyId,
  14. accessKeySecret,
  15. }
  16. // Customizes the connection configuration.
  17. config := sdk.NewConfig()
  18. config.AutoRetry = true // Indicates whether the system automatically retries to connect to ApsaraVideo for VOD upon a connection failure.
  19. config.MaxRetryTime = 3 // The maximum number of retries allowed.
  20. config.Timeout = 3000000000 // The connection timeout duration, in nanoseconds. The default value 3000000000 indicates 3 seconds.
  21. // Creates a vodClient instance.
  22. return vod.NewClientWithOptions(regionId, config, credential)
  23. }
  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. )
  8. func InitVodClient(accessKeyId string, accessKeySecret string, securityToken string) (client *vod.Client, err error) {
  9. // Sets the region of ApsaraVideo for VOD.
  10. regionId := "cn-shanghai"
  11. // Creates an authorization object.
  12. credential := &credentials.StsTokenCredential{
  13. accessKeyId,
  14. accessKeySecret,
  15. securityToken,
  16. }
  17. // Customizes the connection configuration.
  18. config := sdk.NewConfig()
  19. config.AutoRetry = true // Indicates whether the system automatically retries to connect to ApsaraVideo for VOD upon a connection failure.
  20. config.MaxRetryTime = 3 // The maximum number of retries allowed.
  21. config.Timeout = 3000000000 // The connection timeout duration, in nanoseconds. The default value 3000000000 indicates 3 seconds.
  22. // Creates a vodClient instance.
  23. return vod.NewClientWithOptions(regionId, config, credential)
  24. }

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:

  1. func MyGetPlayInfo(client *vod.Client, videoId string) (response *vod.GetPlayInfoResponse, err error) {
  2. // Calls the vod.Create${apiName}Request method to create an API request and sets parameters.
  3. request := vod.CreateGetPlayInfoRequest()
  4. request.VideoId = videoId
  5. request.AcceptFormat = "JSON"
  6. // Calls client.${apiName}(request) to initiate the request and handle exceptions (if any).
  7. return client.GetPlayInfo(request)
  8. }
  9. func main() {
  10. client, err := InitVodClient("<AccessKeyId>", "<AccessKeySecret>")
  11. if err ! = nil {
  12. // Handles exceptions (if any).
  13. panic(err)
  14. }
  15. response, err := MyGetPlayInfo(client, "<videoId>")
  16. if err ! = nil {
  17. // Handles exceptions (if any).
  18. panic(err)
  19. }
  20. // fmt.Println(response)
  21. playList := response.PlayInfoList.PlayInfo
  22. for _, playInfo := range playList {
  23. // The video definition and playback URL.
  24. fmt.Printf("%s: %s\n", playInfo.Definition, playInfo.PlayURL)
  25. }
  26. }

Additional information