全部产品

Go SDK

更新时间:2020-10-15 10:41:58

准备工作

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

完整代码示例

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. tea "github.com/alibabacloud-go/tea-rpc/client"
  7. multisearch "github.com/alibabacloud-go/videosearch-20200225/client"
  8. )
  9. func main() {
  10. config := new(tea.Config)
  11. config.SetAccessKeyId("<yourAccessKeyId>")
  12. config.SetAccessKeySecret("<yourAccessKeySecret>")
  13. config.SetRegionId("<yourRegionId>")
  14. config.SetEndpoint("multisearch.<regionId>.aliyuncs.com")
  15. client, err := multisearch.NewClient(config)
  16. if err != nil {
  17. panic(err)
  18. }
  19. // 创建入库任务
  20. addStorageVideoTask(client)
  21. // 创建检索任务
  22. addSearchVideoTask(client)
  23. // 获取任务状态
  24. getTaskStatus(client)
  25. // 获取入库历史
  26. getStorageHistory(client)
  27. // 删除视频
  28. addDeletionVideoTask(client)
  29. }
  30. func addStorageVideoTask(client *multisearch.Client) {
  31. request := new(multisearch.AddStorageVideoTaskRequest)
  32. // 实例id
  33. request.SetInstanceId("<yourInstanceId>")
  34. // 视频url
  35. request.SetVideoUrl("<yourVideoUrl>")
  36. // 视频id(业务key)
  37. request.SetVideoId("<yourVideoId>")
  38. // 视频描述
  39. request.SetDescription("<yourDescription>")
  40. // 视频标签
  41. request.SetVideoTags("<yourVideoTags>")
  42. // 入库完成的回调
  43. request.SetCallbackUrl("<yourCallbackUrl>")
  44. response, err := client.AddStorageVideoTask(request)
  45. if err != nil {
  46. panic(err)
  47. }
  48. data, err := json.Marshal(response.Data)
  49. if err != nil {
  50. panic(err)
  51. }
  52. fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
  53. fmt.Println(fmt.Sprintf("Data : %s", string(data)))
  54. }
  55. func addSearchVideoTask(client *multisearch.Client) {
  56. request := new(multisearch.AddSearchVideoTaskRequest)
  57. // 实例id
  58. request.SetInstanceId("<yourInstanceId>")
  59. // 检索类型(1. 视频搜视频 2. 图片搜视频)
  60. request.SetSearchType(1)
  61. // 视频/图片的url地址
  62. request.SetVideoUrl("<yourVideoUrl>")
  63. // 视频/图片的描述
  64. request.SetDescription("<yourDescription>")
  65. // 检索标签
  66. request.SetQueryTags("<yourQueryTags>")
  67. // 返回结果数
  68. request.SetReturnVideoNumber(20)
  69. // 查询入库类型,当检索类型为1时生效(1. 直接入库 2. 去重入库 3. 不入库)
  70. request.SetStorageType(3)
  71. // 视频id,当searchType为1且storageType为1,2时生效
  72. request.SetVideoId("<yourVideoId>")
  73. // 视频标签,当searchType为1且storageType为1,2时生效
  74. request.SetVideoTags("<yourVideoTags>")
  75. // 去重入库阈值,searchType为1且storageType为2时生效
  76. request.SetReplaceStorageThreshold(0.75)
  77. // 检索完成的回调
  78. request.SetCallbackUrl("<yourCallbackUrl>")
  79. response, err := client.AddSearchVideoTask(request)
  80. if err != nil {
  81. panic(err)
  82. }
  83. data, err := json.Marshal(response.Data)
  84. if err != nil {
  85. panic(err)
  86. }
  87. fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
  88. fmt.Println(fmt.Sprintf("Data : %s", string(data)))
  89. }
  90. func getTaskStatus(client *multisearch.Client) {
  91. request := new(multisearch.GetTaskStatusRequest)
  92. // 实例id
  93. request.SetInstanceId("<yourInstanceId>")
  94. // 任务id
  95. request.SetTaskId("<yourTaskId>")
  96. response, err := client.GetTaskStatus(request)
  97. if err != nil {
  98. panic(err)
  99. }
  100. data := bytes.NewBuffer([]byte{})
  101. jsonEncoder := json.NewEncoder(data)
  102. jsonEncoder.SetEscapeHTML(false)
  103. err = jsonEncoder.Encode(response.TaskInfo)
  104. if err != nil {
  105. panic(err)
  106. }
  107. fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
  108. fmt.Println(fmt.Sprintf("Data : %d", response.Data))
  109. fmt.Println(fmt.Sprintf("TaskInfo : %s", data.String()))
  110. }
  111. func getStorageHistory(client *multisearch.Client) {
  112. request := new(multisearch.GetStorageHistoryRequest)
  113. // 实例id
  114. request.SetInstanceId("<yourInstanceId>")
  115. // 视频id
  116. request.SetVideoId("<yourVideoId>")
  117. // 当前页数
  118. request.SetPageNumber(1)
  119. // 分页大小
  120. request.SetPageSize(10)
  121. response, err := client.GetStorageHistory(request)
  122. if err != nil {
  123. panic(err)
  124. }
  125. data, err := json.Marshal(response.Data)
  126. fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
  127. fmt.Println(fmt.Sprintf("Data : %s", string(data)))
  128. }
  129. func addDeletionVideoTask(client *multisearch.Client) {
  130. request := new(multisearch.AddDeletionVideoTaskRequest)
  131. // 实例id
  132. request.SetInstanceId("<yourInstanceId>")
  133. // 视频id
  134. request.SetVideoId("<yourVideoId>")
  135. response, err := client.AddDeletionVideoTask(request)
  136. if err != nil {
  137. panic(err)
  138. }
  139. fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
  140. fmt.Println(fmt.Sprintf("Data : %t", *response.Data))
  141. }