全部产品

Go SDK

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参考创建AccessKey

  2. 安装Go SDK。阿里云Go SDK支持Go 1.7及以上版本,您可以通过以下方式安装Go SDK:使用Govendor安装。执行以下命令,安装阿里云Go SDK:

    go get -u github.com/alibabacloud-go/videosearch-20200225/client

    go.mod中引入SDK依赖

    require github.com/alibabacloud-go/videosearch-20200225 v1.1.2

完整代码示例

package main

import(
"bytes"
"encoding/json"
"fmt"
    tea "github.com/alibabacloud-go/tea-rpc/client"
    multisearch "github.com/alibabacloud-go/videosearch-20200225/client"
)

func main(){

    config :=new(tea.Config)
    config.SetAccessKeyId("<yourAccessKeyId>")
    config.SetAccessKeySecret("<yourAccessKeySecret>")
    config.SetRegionId("<yourRegionId>")
    config.SetEndpoint("multisearch.<regionId>.aliyuncs.com")

    client, err := multisearch.NewClient(config)

if err !=nil{
        panic(err)
}

// 创建入库任务
    addStorageVideoTask(client)

// 创建检索任务
    addSearchVideoTask(client)

// 获取任务状态
    getTaskStatus(client)

// 获取入库历史
    getStorageHistory(client)

// 删除视频
    addDeletionVideoTask(client)

 // 查询批量任务
 ListBatchTasks(client)

 //查询音频实例检索任务列表
 ListSearchAudioTasks(client)

 //查询音频实例入库任务列表
 ListStorageAudioTasks(client)

//创建批量任务
 CreateBatchTask(client)
}

func CreateBatchTask(client *multisearch.Client) {
 request := new(multisearch.CreateBatchTaskRequest)
 // 实例id

 request.SetBatchTaskType(1)

 request.SetInstanceId("<yourInstanceId>")

 request.SetRoleArn("<yourRoleArn>")

 request.SetOssBucketName("<yourBucketName>")

 request.SetOssDataPath("<yourOssDataPath>")

 request.SetOssMetaFile("<yourOssMetaFile>")

 response, err := client.CreateBatchTask(request,new(service.RuntimeOptions))
 if err != nil {
  panic(err)
 }
 data, err := json.Marshal(response)
 if err != nil {
  panic(err)
 }
 fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
 fmt.Println(fmt.Sprintf("Data : %s", string(data)))
}

func ListStorageAudioTasks(client *multisearch.Client) {
 request := new(multisearch.ListStorageAudioTasksRequest)
 // 实例id
 request.SetInstanceId("<yourInstanceId>")

 response, err := client.ListStorageAudioTasks(request,new(service.RuntimeOptions))
 if err != nil {
  panic(err)
 }
 data, err := json.Marshal(response.Data)
 if err != nil {
  panic(err)
 }
 fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
 fmt.Println(fmt.Sprintf("Data : %s", string(data)))
}

func ListSearchAudioTasks(client *multisearch.Client) {
 request := new(multisearch.ListSearchAudioTasksRequest)
 // 实例id
 request.SetInstanceId("<yourInstanceId>")

 response, err := client.ListSearchAudioTasks(request,new(service.RuntimeOptions))
 if err != nil {
  panic(err)
 }
 data, err := json.Marshal(response.Data)
 if err != nil {
  panic(err)
 }
 fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
 fmt.Println(fmt.Sprintf("Data : %s", string(data)))
}

func ListBatchTasks(client *multisearch.Client) {
 request := new(multisearch.ListBatchTaskRequest)
 // 实例id
 request.SetInstanceId("<yourInstanceId>")

 response, err := client.ListBatchTask(request,new(service.RuntimeOptions))
 if err != nil {
  panic(err)
 }
 data, err := json.Marshal(response.Data)
 if err != nil {
  panic(err)
 }
 fmt.Println(fmt.Sprintf("RequestId : %s", *response.RequestId))
 fmt.Println(fmt.Sprintf("Data : %s", string(data)))
}



func addStorageVideoTask(client *multisearch.Client){
    request :=new(multisearch.AddStorageVideoTaskRequest)
// 实例id
    request.SetInstanceId("<yourInstanceId>")
// 视频url
    request.SetVideoUrl("<yourVideoUrl>")
// 视频id(业务key)
    request.SetVideoId("<yourVideoId>")
// 视频描述
    request.SetDescription("<yourDescription>")
// 视频标签
    request.SetVideoTags("<yourVideoTags>")
// 入库完成的回调
    request.SetCallbackUrl("<yourCallbackUrl>")

    response, err := client.AddStorageVideoTask(request)

if err !=nil{
        panic(err)
}

    data, err := json.Marshal(response.Data)
if err !=nil{
        panic(err)
}
    fmt.Println(fmt.Sprintf("RequestId : %s",*response.RequestId))
    fmt.Println(fmt.Sprintf("Data : %s",string(data)))
}

func addSearchVideoTask(client *multisearch.Client){
    request :=new(multisearch.AddSearchVideoTaskRequest)

// 实例id
    request.SetInstanceId("<yourInstanceId>")
// 检索类型(1. 视频搜视频   2. 图片搜视频)
    request.SetSearchType(1)
// 视频/图片的url地址
//如果需要使用 本地文件上传的功能,则用xxxFileObject参数并且使用 AddSearchXXXTaskAdvanceRequest对象
    request.SetVideoUrl("<yourVideoUrl>")
// 视频/图片的描述
    request.SetDescription("<yourDescription>")
// 检索标签
    request.SetQueryTags("<yourQueryTags>")
// 返回结果数
    request.SetReturnVideoNumber(20)
// 查询入库类型,当检索类型为1时生效(1. 直接入库  2. 去重入库  3. 不入库)
    request.SetStorageType(3)
// 视频id,当searchType为1且storageType为1,2时生效
    request.SetVideoId("<yourVideoId>")
// 视频标签,当searchType为1且storageType为1,2时生效
    request.SetVideoTags("<yourVideoTags>")
// 去重入库阈值,searchType为1且storageType为2时生效
    request.SetReplaceStorageThreshold(0.75)
// 检索完成的回调
    request.SetCallbackUrl("<yourCallbackUrl>")

    response, err := client.AddSearchVideoTask(request)
if err !=nil{
        panic(err)
}

    data, err := json.Marshal(response.Data)
if err !=nil{
        panic(err)
}
    fmt.Println(fmt.Sprintf("RequestId : %s",*response.RequestId))
    fmt.Println(fmt.Sprintf("Data : %s",string(data)))
}

func getTaskStatus(client *multisearch.Client){
    request :=new(multisearch.GetTaskStatusRequest)
// 实例id
    request.SetInstanceId("<yourInstanceId>")
// 任务id
    request.SetTaskId("<yourTaskId>")

    response, err := client.GetTaskStatus(request)
if err !=nil{
        panic(err)
}

    data := bytes.NewBuffer([]byte{})
    jsonEncoder := json.NewEncoder(data)
    jsonEncoder.SetEscapeHTML(false)
    err = jsonEncoder.Encode(response.TaskInfo)
if err !=nil{
        panic(err)
}
    fmt.Println(fmt.Sprintf("RequestId : %s",*response.RequestId))
    fmt.Println(fmt.Sprintf("Data : %d", response.Data))
    fmt.Println(fmt.Sprintf("TaskInfo : %s", data.String()))
}

func getStorageHistory(client *multisearch.Client){
    request :=new(multisearch.GetStorageHistoryRequest)
// 实例id
    request.SetInstanceId("<yourInstanceId>")
// 视频id
    request.SetVideoId("<yourVideoId>")
// 当前页数
    request.SetPageNumber(1)
// 分页大小
    request.SetPageSize(10)

    response, err := client.GetStorageHistory(request)
if err !=nil{
        panic(err)
}

    data, err := json.Marshal(response.Data)
    fmt.Println(fmt.Sprintf("RequestId : %s",*response.RequestId))
    fmt.Println(fmt.Sprintf("Data : %s",string(data)))
}

func addDeletionVideoTask(client *multisearch.Client){
    request :=new(multisearch.AddDeletionVideoTaskRequest)
// 实例id
    request.SetInstanceId("<yourInstanceId>")
// 视频id
    request.SetVideoId("<yourVideoId>")

    response, err := client.AddDeletionVideoTask(request)
if err !=nil{
        panic(err)
}

    fmt.Println(fmt.Sprintf("RequestId : %s",*response.RequestId))
    fmt.Println(fmt.Sprintf("Data : %t",*response.Data))
}