This topic describes how to use Alibaba Cloud Image Search SDKs for Node.js.

Preparations

  • Before you install and use Alibaba Cloud SDKs, make sure that you have created an Alibaba Cloud account and obtained an AccessKey pair. For more information, see Create an AccessKey pair.
  • Install the core library of the Alibaba Cloud SDK for Node.js. The Alibaba Cloud SDK for Node.js supports Node.js 8.0 or later. You can run the following command to install @alicloud/pop-core:
    npm install @alicloud/pop-core -S
  • You can also use package managers such as cnpm and Yarn to install @alicloud/pop-core.
  • Run the following command to install the Alibaba Cloud Image Search SDK for Node.js:
    npm install @alicloud/imagesearch-2019-03-25  --save

Sample code

The following complete sample code is provided:
'use strict';
const fs = require('fs');
const qs = require("querystring");
const Client = require("@alicloud/imagesearch-2019-03-25");
const client = new Client({
  accessKeyId: "<your-access-key-id>",
  accessKeySecret: "<your-access-key-secret>",
  endpoint: "http://imagesearch.<region>.aliyuncs.com"
});
const options = {
  method: 'POST',
  "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8'
};
var picContent = fs.readFileSync("/home/admin/demo.jpg").toString("base64");
async function demo() {
  // Add the image.
  const addRequest = {
    InstanceName: "demo",
    ProductId: "test",
    PicName: "test",
    PicContent: picContent
  };
  const addData = qs.stringify(addRequest);
  const addResponse = await client.addImage(addData, options);
  console.log(1, addResponse);
  // Search for images.
  const searchRequest = {
    InstanceName: "test",
    PicContent: picContent
  };
  const searchData = qs.stringify(searchRequest);
  const searchResponse = await client.searchImage(searchData, options);
  console.log(2, searchResponse);
  // Delete the image.
  const deleteRequest = {
    InstanceName: "demo",
    ProductId: "test"
  };
  const deleteData = qs.stringify(deleteRequest);
  const deleteResponse = await client.deleteImage(deleteData, options);
  console.log(3, deleteResponse);
}
demo();