全部产品
云市场
云游戏

Go SDK

更新时间:2020-04-24 14:42:58

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参考创建AccessKey
  2. 安装Go SDK。阿里云Go SDK支持Go 1.7及以上版本,您可以通过以下方式安装Go SDK:
    使用Govendor安装。
    执行以下命令,安装阿里云Go SDK:
    1. go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk

完整代码示例

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  6. "github.com/aliyun/alibaba-cloud-sdk-go/services/videosearch"
  7. )
  8. func main() {
  9. // 创建client实例
  10. client, err := videosearch.NewClientWithAccessKey(
  11. "<region>", // 地域ID 目前支持cn-beijing
  12. "<your-access-key-id>", // RAM账号的AccessKey ID
  13. "<your-access-key-secret>") // RAM账号Access Key Secret
  14. if err != nil {
  15. // 异常处理
  16. panic(err)
  17. }
  18. endpoints.AddEndpointMapping("cn-beijing", "videosearch", "multisearch.cn-beijing.aliyuncs.com")
  19. // 创建视频入库任务
  20. addStorageVideoTaskRequest := videosearch.CreateAddStorageVideoTaskRequest()
  21. // 处理任务的实例id
  22. addStorageVideoTaskRequest.InstanceId = "<yourInstanceId>"
  23. // 视频oss url
  24. addStorageVideoTaskRequest.VideoUrl = "<yourVideoUrl>"
  25. // 视频的业务key
  26. addStorageVideoTaskRequest.VideoId = "<yourVideoId>"
  27. // 视频的业务标签,此参数为可选
  28. addStorageVideoTaskRequest.VideoTags = "<yourVideoTag>"
  29. // 入库回调地址,此参数为可选
  30. addStorageVideoTaskRequest.CallbackUrl = "<yourCallbackUrl>"
  31. addStorageVideoTaskResponse, addStorageVideoTaskErr := client.AddStorageVideoTask(request)
  32. if addStorageVideoTaskErr != nil {
  33. panic(addStorageVideoTaskErr)
  34. }
  35. fmt.Println(addStorageVideoTaskResponse)
  36. // 创建视频入库任务
  37. addSearchVideoTaskRequest := videosearch.CreateAddSearchVideoTaskRequest()
  38. // 处理任务的实例id
  39. addSearchVideoTaskRequest.InstanceId = "<yourInstanceId>"
  40. // 视频oss url
  41. addSearchVideoTaskRequest.VideoUrl = "<yourVideoUrl>"
  42. // 视频的业务key,当StorageType为1(直接入库)、2(覆盖入库)时为必填
  43. addSearchVideoTaskRequest.VideoId = "<yourVideoId>"
  44. // 视频的业务标签,当StorageType为1(直接入库)、2(覆盖入库)时生效
  45. addSearchVideoTaskRequest.VideoTags = "<yourVideoTag>"
  46. // 返回视频数量
  47. addSearchVideoTaskRequest.ReturnVideoNumber = requests.NewInteger(10)
  48. // 检索入库类型,取值范围为1(直接入库)、2(覆盖入库)、3(不入库)
  49. addSearchVideoTaskRequest.StorageType = requests.NewInteger(3)
  50. // 查询标签,对应视频入库时的videoTag
  51. addSearchVideoTaskRequest.QueryTags = "<yourQueryTags>"
  52. // 覆盖入库的阈值,当searchStorageType为2(覆盖入库)时生效
  53. addSearchVideoTaskRequest.ReplaceStorageThreshold = requests.NewFloat(0.95)
  54. addSearchVideoTaskRequest.CallbackUrl = "<yourCallbackUrl>"
  55. addSearchVideoTaskResponse, addSearchVideoTaskErr := client.AddSearchVideoTask(request)
  56. if addSearchVideoTaskErr != nil {
  57. panic(addSearchVideoTaskErr)
  58. }
  59. fmt.Println(addSearchVideoTaskResponse)
  60. // 查询任务状态
  61. getTaskStatusRequest := videosearch.CreateGetTaskStatusRequest()
  62. // 处理任务的实例id
  63. getTaskStatusRequest.InstanceId = "<yourInstanceId>"
  64. // 任务id
  65. getTaskStatusRequest.TaskId = "<yourTaskId>"
  66. getTaskStatusResponse, getTaskStatusErr := client.GetTaskStatus(getTaskStatusRequest)
  67. if getTaskStatusErr != nil {
  68. panic(getTaskStatusErr)
  69. }
  70. fmt.Println(getTaskStatusResponse)
  71. // 删除视频
  72. addDeletionVideoTaskRequest := videosearch.CreateAddDeletionVideoTaskRequest()
  73. // 处理任务的实例id
  74. addDeletionVideoTaskRequest.InstanceId = "<yourInstanceId>"
  75. addDeletionVideoTaskRequest.VideoId = "<yourVideoId>"
  76. addDeletionVideoTaskResponse, addDeletionVideoTaskErr := client.AddDeletionVideoTask(addDeletionVideoTaskRequest)
  77. if addDeletionVideoTaskErr != nil {
  78. panic(addDeletionVideoTaskErr)
  79. }
  80. fmt.Println(addDeletionVideoTaskResponse)
  81. }