全部产品

Node.js SDK

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

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参考创建AccessKey
  2. 安装node.js SDK。您可以通过以下方式安装node.js SDK:
    使用依赖管理工具安装。
    执行以下命令,安装阿里云node.js SDK:
    1. npm install @alicloud/videosearch20200225@1.0.0

完整代码示例

  1. const teaClient = require('@alicloud/rpc-client')
  2. const multisearch = require('@alicloud/videosearch20200225')
  3. const config = new teaClient.Config({
  4. accessKeyId: "<yourAccessKeyId>",
  5. accessKeySecret: "<yourAccessKeySecret>",
  6. regionId: "<yourRegionId>",
  7. endpoint: "multisearch.<regionId>.aliyuncs.com",
  8. })
  9. const client = new multisearch.default(config)
  10. // 创建视频入库任务
  11. async function addStorageVideoTask() {
  12. const addStorageVideoTaskRequest = new multisearch.AddStorageVideoTaskRequest()
  13. // 实例id
  14. addStorageVideoTaskRequest.instanceId = "<yourInstanceId>"
  15. // 视频url
  16. addStorageVideoTaskRequest.videoUrl = "<yourVideoUrl>"
  17. // 视频id(业务key)
  18. addStorageVideoTaskRequest.videoId = "<yourVideoId>"
  19. // 视频描述
  20. addStorageVideoTaskRequest.description = "<yourDescription>"
  21. // 视频标签
  22. addStorageVideoTaskRequest.videoTags = "<yourVideoTags>"
  23. // 入库完成的回调
  24. addStorageVideoTaskRequest.callbackUrl = "<yourCallbackUrl>"
  25. const addStorageVideoTaskResponse = await client.addStorageVideoTask(addStorageVideoTaskRequest)
  26. console.log(addStorageVideoTaskResponse)
  27. }
  28. addStorageVideoTask();
  29. // 创建视频检索任务
  30. async function addSearchVideoTask() {
  31. const addSearchVideoTaskRequest = new multisearch.AddSearchVideoTaskRequest()
  32. // 实例id
  33. addSearchVideoTaskRequest.instanceId = "<yourInstanceId>"
  34. // 检索类型(1. 视频搜视频 2. 图片搜视频)
  35. addSearchVideoTaskRequest.searchType = 1
  36. // 视频/图片的url地址
  37. addSearchVideoTaskRequest.videoUrl = "<yourVideoUrl>"
  38. // 视频/图片的描述
  39. addSearchVideoTaskRequest.description = "<yourDescription>"
  40. // 检索标签
  41. addSearchVideoTaskRequest.queryTags = "<yourQueryTags>"
  42. // 返回结果数
  43. addSearchVideoTaskRequest.returnVideoNumber = 20
  44. // 查询入库类型,当检索类型为1时生效(1. 直接入库 2. 去重入库 3. 不入库)
  45. addSearchVideoTaskRequest.storageType = 3
  46. // 视频id,当searchType为1且storageType为1,2时生效
  47. addSearchVideoTaskRequest.videoId = "<yourVideoId>"
  48. // 视频标签,当searchType为1且storageType为1,2时生效
  49. addSearchVideoTaskRequest.videoTags = "<yourVideoTags>"
  50. // 去重入库阈值,searchType为1且storageType为2时生效
  51. addSearchVideoTaskRequest.replaceStorageThreshold = 0.75
  52. // 检索完成的回调
  53. addSearchVideoTaskRequest.callbackUrl = "<yourCallbackUrl>"
  54. const addSearchVideoTaskResponse = await client.addSearchVideoTask(addSearchVideoTaskRequest)
  55. console.log(addSearchVideoTaskResponse)
  56. }
  57. addSearchVideoTask()
  58. // 获取视频状态
  59. async function getTaskStatus() {
  60. const getTaskStatusRequest = new multisearch.GetTaskStatusRequest()
  61. // 实例id
  62. getTaskStatusRequest.instanceId = "<yourInstanceId>"
  63. // 任务id
  64. getTaskStatusRequest.taskId = "<yourTaskId>"
  65. const getTaskStatusResponse = await client.getTaskStatus(getTaskStatusRequest)
  66. console.log(getTaskStatusResponse)
  67. }
  68. getTaskStatus()
  69. // 获取入库记录
  70. async function getStorageHistory() {
  71. const getStorageHistoryRequest = new multisearch.GetStorageHistoryRequest()
  72. // 实例id
  73. getStorageHistoryRequest.instanceId = "<yourInstanceId>"
  74. // 视频id
  75. getStorageHistoryRequest.videoId = "<yourVideoId>"
  76. // 当前页数
  77. getStorageHistoryRequest.pageNumber = 1
  78. // 分页大小
  79. getStorageHistoryRequest.pageSize = 10
  80. const getStorageHistoryResponse = await client.getStorageHistory(getStorageHistoryRequest)
  81. console.log(getStorageHistoryResponse)
  82. }
  83. getStorageHistory()
  84. // 删除任务
  85. async function addDeletionVideoTask() {
  86. const addDeletionVideoTaskRequest = new multisearch.AddDeletionVideoTaskRequest()
  87. // 实例id
  88. addDeletionVideoTaskRequest.instanceId = "<yourInstanceId>"
  89. // 视频id
  90. addDeletionVideoTaskRequest.videoId = "<yourVideoId>"
  91. const addDeletionVideoTaskResponse = await client.addDeletionVideoTask(addDeletionVideoTaskRequest)
  92. console.log(addDeletionVideoTaskResponse)
  93. }
  94. addDeletionVideoTask()