全部产品

.NET SDK

更新时间:2020-10-12 17:23:55

准备工作

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

  2. 将 SDK添加到项目中
    通过 NuGet程序包管理器来安装,在解决方案资源管理器面板中右击您的项目选择管理 NuGet程序包菜单,在打开的 NuGet管理面板中点击浏览选项卡输入:

    1. AlibabaCloud.SDK.Videosearch20200225

    选择并点击安装即可。

完整代码示例

  1. using System;
  2. using AlibabaCloud.RPCClient.Models;
  3. using AlibabaCloud.SDK.Videosearch20200225;
  4. using AlibabaCloud.SDK.Videosearch20200225.Models;
  5. using Newtonsoft.Json;
  6. using Tea;
  7. namespace SdkTest
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Config config = new Config();
  14. config.AccessKeyId = "<yourAccessKeyId>";
  15. config.AccessKeySecret = "<yourAccessKeySecret>";
  16. config.RegionId = "<yourRegionId>";
  17. config.Endpoint = "multisearch.<regionId>.aliyuncs.com";
  18. Client client = new Client(config);
  19. // 创建视频入库任务
  20. AddStorageVideoTask(client);
  21. // 创建检索任务
  22. addSearchVideoTask(client);
  23. // 获取任务状态
  24. getTaskStatus(client);
  25. // 获取入库历史
  26. getStorageHistory(client);
  27. // 删除视频
  28. addDeletionVideoTask(client);
  29. }
  30. private static void AddStorageVideoTask(Client client)
  31. {
  32. AddStorageVideoTaskRequest request = new AddStorageVideoTaskRequest();
  33. // 实例id
  34. request.InstanceId = "<yourInstanceId>";
  35. // 视频url
  36. request.VideoUrl = "<yourVideoUrl>";
  37. // 视频id(业务key)
  38. request.VideoId = "<yourVideoId>";
  39. // 视频描述
  40. request.Description = "<yourDescription>";
  41. // 视频标签
  42. request.VideoTags = "<yourVideoTags>";
  43. // 入库完成的回调
  44. request.CallbackUrl = "<yourCallbackUrl>";
  45. try
  46. {
  47. AddStorageVideoTaskResponse response = client.AddStorageVideoTask(request);
  48. Console.WriteLine("requestId : " + response.RequestId);
  49. Console.WriteLine("Data : " + JsonConvert.SerializeObject(response.Data));
  50. }
  51. catch (TeaUnretryableException e)
  52. {
  53. if (e.InnerException != null)
  54. {
  55. Console.WriteLine("InnerException结果:" + e.InnerException.Message);
  56. }
  57. Console.WriteLine("Data结果:" + e.Data);
  58. Console.WriteLine("Message结果:" + e.Message);
  59. Console.WriteLine("打印String:" + e);
  60. }
  61. }
  62. private static void addSearchVideoTask(Client client)
  63. {
  64. AddSearchVideoTaskRequest request = new AddSearchVideoTaskRequest();
  65. // 实例id
  66. request.InstanceId = "<yourInstanceId>";
  67. // 检索类型(1. 视频搜视频 2. 图片搜视频)
  68. request.SearchType = 1;
  69. // 视频/图片的url地址
  70. request.VideoUrl = "<yourVideoUrl>";
  71. // 视频/图片的描述
  72. request.Description = "<yourDescription>";
  73. // 检索标签
  74. request.QueryTags = "<yourQueryTags>";
  75. // 返回结果数
  76. request.ReturnVideoNumber = 20;
  77. // 查询入库类型,当检索类型为1时生效(1. 直接入库 2. 去重入库 3. 不入库)
  78. request.StorageType = 3;
  79. // 视频id,当searchType为1且storageType为1,2时生效
  80. request.VideoId = "<yourVideoId>";
  81. // 视频标签,当searchType为1且storageType为1,2时生效
  82. request.VideoTags = "<yourVideoTags>";
  83. // 去重入库阈值,searchType为1且storageType为2时生效
  84. request.ReplaceStorageThreshold = 0.75F;
  85. // 检索完成的回调
  86. request.CallbackUrl = "<yourCallbackUrl>";
  87. try
  88. {
  89. AddSearchVideoTaskResponse response = client.AddSearchVideoTask(request);
  90. Console.WriteLine("requestId : " + response.RequestId);
  91. Console.WriteLine("Data : " + JsonConvert.SerializeObject(response.Data));
  92. }
  93. catch (TeaUnretryableException e)
  94. {
  95. if (e.InnerException != null)
  96. {
  97. Console.WriteLine("InnerException结果:" + e.InnerException.Message);
  98. }
  99. Console.WriteLine("Data结果:" + e.Data);
  100. Console.WriteLine("Message结果:" + e.Message);
  101. Console.WriteLine("打印String:" + e);
  102. }
  103. }
  104. private static void getTaskStatus(Client client)
  105. {
  106. GetTaskStatusRequest request = new GetTaskStatusRequest();
  107. // 实例id
  108. request.InstanceId = "<yourInstanceId>";
  109. // 任务id
  110. request.TaskId = "<yourTaskId>";
  111. try
  112. {
  113. GetTaskStatusResponse response = client.GetTaskStatus(request);
  114. Console.WriteLine("requestId : " + response.RequestId);
  115. Console.WriteLine("Data : " + response.Data);
  116. Console.WriteLine("TaskInfo : " + JsonConvert.SerializeObject(response.TaskInfo));
  117. }
  118. catch (TeaUnretryableException e)
  119. {
  120. if (e.InnerException != null)
  121. {
  122. Console.WriteLine("InnerException结果:" + e.InnerException.Message);
  123. }
  124. Console.WriteLine("Data结果:" + e.Data);
  125. Console.WriteLine("Message结果:" + e.Message);
  126. Console.WriteLine("打印String:" + e);
  127. }
  128. }
  129. private static void getStorageHistory(Client client)
  130. {
  131. GetStorageHistoryRequest request = new GetStorageHistoryRequest();
  132. // 实例id
  133. request.InstanceId = "<yourInstanceId>";
  134. // 视频id
  135. request.VideoId = "<yourVideoId>";
  136. // 当前页数
  137. request.PageNumber = 1;
  138. // 分页大小
  139. request.PageSize = 10;
  140. try
  141. {
  142. GetStorageHistoryResponse response = client.GetStorageHistory(request);
  143. Console.WriteLine("requestId : " + response.RequestId);
  144. Console.WriteLine("Data : " + JsonConvert.SerializeObject(response.Data));
  145. }
  146. catch (TeaUnretryableException e)
  147. {
  148. if (e.InnerException != null)
  149. {
  150. Console.WriteLine("InnerException结果:" + e.InnerException.Message);
  151. }
  152. Console.WriteLine("Data结果:" + e.Data);
  153. Console.WriteLine("Message结果:" + e.Message);
  154. Console.WriteLine("打印String:" + e);
  155. }
  156. }
  157. private static void addDeletionVideoTask(Client client)
  158. {
  159. AddDeletionVideoTaskRequest request = new AddDeletionVideoTaskRequest();
  160. // 实例id
  161. request.InstanceId = "<yourInstanceId>";
  162. // 视频id
  163. request.VideoId = "<yourVideoId>";
  164. try
  165. {
  166. AddDeletionVideoTaskResponse response = client.AddDeletionVideoTask(request);
  167. Console.WriteLine("requestId : " + response.RequestId);
  168. Console.WriteLine("Data : " + response.Data);
  169. }
  170. catch (TeaUnretryableException e)
  171. {
  172. if (e.InnerException != null)
  173. {
  174. Console.WriteLine("InnerException结果:" + e.InnerException.Message);
  175. }
  176. Console.WriteLine("Data结果:" + e.Data);
  177. Console.WriteLine("Message结果:" + e.Message);
  178. Console.WriteLine("打印String:" + e);
  179. }
  180. }
  181. }
  182. }