全部产品
云市场

Node.js SDK

更新时间:2019-04-16 20:08:48

准备工作

  1. 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问访问密钥(AccessKey)。详情参考创建AccessKey
  2. 安装Node.js SDK核心库。阿里云Node.js SDK支持的版本为8.0及以上,您可以通过以下方式安装Node.js SDK:
    1. npm install @alicloud/pop-core -S
    您也可以使用cnpm、yarn等包管理工具来安装@alicloud/pop-core。
  3. 安装图像搜索Node.js SDK。
    1. npm install @alicloud/imagesearch-2019-03-25 --save

完整代码示例

  1. 'use strict';
  2. const fs = require('fs');
  3. const qs = require("querystring");
  4. const Client = require("@alicloud/imagesearch-2019-03-25");
  5. const client = new Client({
  6. accessKeyId: "<your-access-key-id>",
  7. accessKeySecret: "<your-access-key-secret>",
  8. endpoint: "http://imagesearch.<region>.aliyuncs.com"
  9. });
  10. const options = {
  11. method: 'POST',
  12. "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8'
  13. };
  14. var picContent = fs.readFileSync("/home/admin/demo.jpg").toString("base64");
  15. async function demo() {
  16. // 添加图片
  17. const addRequest = {
  18. InstanceName: "demo",
  19. ProductId: "test",
  20. PicName: "test",
  21. PicContent: picContent
  22. };
  23. const addData = qs.stringify(addRequest);
  24. const addResponse = await client.addImage(addData, options);
  25. console.log(1, addResponse);
  26. // 查询图片
  27. const searchRequest = {
  28. InstanceName: "test",
  29. PicContent: picContent
  30. };
  31. const searchData = qs.stringify(searchRequest);
  32. const searchResponse = await client.searchImage(searchData, options);
  33. console.log(2, searchResponse);
  34. // 删除图片
  35. const deleteRequest = {
  36. InstanceName: "demo",
  37. ProductId: "test"
  38. };
  39. const deleteData = qs.stringify(deleteRequest);
  40. const deleteResponse = await client.deleteImage(deleteData, options);
  41. console.log(3, deleteResponse);
  42. }
  43. demo();