All Products
Search
Document Center

Node.js SDK

Last Updated: Apr 28, 2019

Preparations

  1. Before installing and using Alibaba Cloud SDKs, make sure that you have registered an Alibaba Cloud account and generated an AccessKey. For more information, see Create an AccessKey.

  2. Install the Node.js SDK. The Alibaba Cloud Node.js SDK supports Node.js 8.0 and later versions. You can run the following command to install the Node.js SDK:

    1. npm install @alicloud/pop-core -S

    You can also use tools such as cnpm and Yarn to install @alicloud/pop-core.

  3. Install the Image Search Node.js SDK.

    1. npm install @alicloud/imagesearch-2019-03-25 --save
  4. We recommend that you install the following Node.js SDK.

    1. npm install json-to-form-data --save

Sample code

  1. var Client = require("@alicloud/imagesearch-2019-03-25");
  2. const jtfd = require("json-to-form-data");
  3. const fs = require('fs');
  4. var client = new Client({
  5. accessKeyId: "<your-access-key-id>",
  6. accessKeySecret: "<your-access-key-secret>",
  7. endpoint: "http://imagesearch.<region>.aliyuncs.com",
  8. apiVersion: "2019-03-25"
  9. });
  10. var 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. //Add an image.
  16. var addRequest = {
  17. InstanceName: "demo",
  18. ProductId: "test",
  19. PicName: "test",
  20. PicContent: picContent
  21. };
  22. var addData = jtfd(addRequest)
  23. client.addImage(addData, options).then(function (value) {
  24. console.log("Result", JSON.stringify(value));
  25. }).catch(function (err) {
  26. console.log("Error Message: ", err);
  27. });
  28. //Search for an image.
  29. var searchRequest = {
  30. InstanceName: "demo",
  31. PicContent: picContent
  32. };
  33. var searchData = jtfd(searchRequest)
  34. client.searchImage(searchData, options).then(function (value) {
  35. console.log("Result", JSON.stringify(value));
  36. }).catch(function (err) {
  37. console.log("Error Message: ", err);
  38. });
  39. // Delete an image.
  40. var deleteRequest = {
  41. InstanceName: "demo",
  42. ProductId: "test"
  43. };
  44. var deleteData = jtfd(deleteRequest)
  45. client.deleteImage(deleteData, options).then(function (value) {
  46. console.log("Result", JSON.stringify(value));
  47. }).catch(function (err) {
  48. console.log("Error Message: ", err);
  49. });