全部产品

Node.js SDK

准备工作

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

  2. 安装node.js SDK。您可以通过以下方式安装node.js SDK:使用依赖管理工具安装。执行以下命令,安装阿里云node.js SDK:

    npm install  @alicloud/videosearch20200225@1.1.2

完整代码示例

const teaClient = require('@alicloud/rpc-client')
const multisearch = require('@alicloud/videosearch20200225')

const config =new teaClient.Config({
    accessKeyId:"<yourAccessKeyId>",
    accessKeySecret:"<yourAccessKeySecret>",
    regionId:"<yourRegionId>",
    endpoint:"multisearch.<regionId>.aliyuncs.com",
})

const client =new multisearch.default(config)

// 创建视频入库任务
async function addStorageVideoTask(){
const addStorageVideoTaskRequest =new multisearch.AddStorageVideoTaskRequest()
// 实例id
    addStorageVideoTaskRequest.instanceId ="<yourInstanceId>"
// 视频url
    addStorageVideoTaskRequest.videoUrl ="<yourVideoUrl>"
// 视频id(业务key)
    addStorageVideoTaskRequest.videoId ="<yourVideoId>"
// 视频描述
    addStorageVideoTaskRequest.description ="<yourDescription>"
// 视频标签
    addStorageVideoTaskRequest.videoTags ="<yourVideoTags>"
// 入库完成的回调
    addStorageVideoTaskRequest.callbackUrl ="<yourCallbackUrl>"

const addStorageVideoTaskResponse = await client.addStorageVideoTask(addStorageVideoTaskRequest)

    console.log(addStorageVideoTaskResponse)
}

addStorageVideoTask();

// 创建视频检索任务
async function addSearchVideoTask(){
const addSearchVideoTaskRequest =new multisearch.AddSearchVideoTaskRequest()
// 实例id
    addSearchVideoTaskRequest.instanceId ="<yourInstanceId>"
// 检索类型(1. 视频搜视频   2. 图片搜视频)
    addSearchVideoTaskRequest.searchType =1
// 视频/图片的url地址
//如果需要使用 本地文件上传的功能,则用xxxFileObject参数并且使用 AddSearchXXXTaskAdvanceRequest对象
    addSearchVideoTaskRequest.videoUrl ="<yourVideoUrl>"
// 视频/图片的描述
    addSearchVideoTaskRequest.description ="<yourDescription>"
// 检索标签
    addSearchVideoTaskRequest.queryTags ="<yourQueryTags>"
// 返回结果数
    addSearchVideoTaskRequest.returnVideoNumber =20
// 查询入库类型,当检索类型为1时生效(1. 直接入库  2. 去重入库  3. 不入库)
    addSearchVideoTaskRequest.storageType =3
// 视频id,当searchType为1且storageType为1,2时生效
      addSearchVideoTaskRequest.videoId ="<yourVideoId>"
// 视频标签,当searchType为1且storageType为1,2时生效
    addSearchVideoTaskRequest.videoTags ="<yourVideoTags>"
// 去重入库阈值,searchType为1且storageType为2时生效
    addSearchVideoTaskRequest.replaceStorageThreshold =0.75
// 检索完成的回调
    addSearchVideoTaskRequest.callbackUrl ="<yourCallbackUrl>"

const addSearchVideoTaskResponse = await client.addSearchVideoTask(addSearchVideoTaskRequest)

    console.log(addSearchVideoTaskResponse)
}

addSearchVideoTask()

// 获取视频状态
async function getTaskStatus(){
const getTaskStatusRequest =new multisearch.GetTaskStatusRequest()
// 实例id
    getTaskStatusRequest.instanceId ="<yourInstanceId>"
// 任务id
    getTaskStatusRequest.taskId ="<yourTaskId>"

const getTaskStatusResponse = await client.getTaskStatus(getTaskStatusRequest)

    console.log(getTaskStatusResponse)
}

getTaskStatus()

// 获取入库记录
async function getStorageHistory(){
const getStorageHistoryRequest =new multisearch.GetStorageHistoryRequest()
// 实例id
    getStorageHistoryRequest.instanceId ="<yourInstanceId>"
// 视频id
    getStorageHistoryRequest.videoId ="<yourVideoId>"
// 当前页数
    getStorageHistoryRequest.pageNumber =1
// 分页大小
    getStorageHistoryRequest.pageSize =10

const getStorageHistoryResponse = await client.getStorageHistory(getStorageHistoryRequest)

    console.log(getStorageHistoryResponse)
}

getStorageHistory()

// 删除任务
async function addDeletionVideoTask(){
const addDeletionVideoTaskRequest =new multisearch.AddDeletionVideoTaskRequest()
// 实例id
    addDeletionVideoTaskRequest.instanceId ="<yourInstanceId>"
// 视频id
    addDeletionVideoTaskRequest.videoId ="<yourVideoId>"

const addDeletionVideoTaskResponse = await client.addDeletionVideoTask(addDeletionVideoTaskRequest)

    console.log(addDeletionVideoTaskResponse)
}

addDeletionVideoTask()