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 Alibaba Cloud Image Search SDK for Node.js. The Node.js version must be 6.X or later.
    $ npm install @alicloud/imagesearch-2018-01-20  --save

Create a client

The following sample code is used to create a client:
var Client = require("@alicloud/imagesearch-2018-01-20");
var client = new Client({
  accessKeyId: "<AccessKeyID>", // The AccessKey ID. You can obtain the AccessKey ID from https://ak-console.aliyun.com.
  accessKeySecret: "<AccessKeySecret>",
  endpoint: "http://imagesearch.ap-southeast-1.aliyuncs.com", // The endpoint. For more information, see https://www.alibabacloud.com/help/doc-detail/179213.html.
  apiVersion: "2018-01-20"
});

Add images

For product image searches, you can specify or do not specify category IDs. For generic image searches, you do not need to specify category IDs. The following sample code is used to add images:
var instanceName = "<InstanceName>"; // The name of the purchased Image Search instance, for example, imagesearchtest.
var catId = "0"; // The category ID. For more information, see https://www.alibabacloud.com/help/doc-detail/179184.html.
var itemId = "1234"; // The ID of the product or the image.
var custContent = "{\"key\":\"value\"}"; // The metadata of the product or the image. The metadata is pass-through in the search results.
var picList = {}; // The list of images.

// The 1.jpg and 2.jpg images in the current directory are used as examples.
picList[new Buffer("1.jpg").toString("base64")] = fs.readFileSync(path.resolve(__dirname, "1.jpg")).toString("base64");
picList[new Buffer("2.jpg").toString("base64")] = fs.readFileSync(path.resolve(__dirname, "2.jpg")).toString("base64");

var buildAddContent = function () {
  if (! itemId || ! catId || Object.keys(picList).length <= 0) {
    return ;
  }
  
  if (custContent == null) {
    custContent = "";
  }
  
  var params = {};
  
  params.item_id = itemId;
  params.cat_id = catId + "";
  params.cust_content = custContent;
  
  var picListStr = "";
  
  Object.keys(picList).forEach(function (picName) {
    if (picList[picName].length <= 0) {
      return;
    }
    picListStr += picName + ",";
    params[picName] = picList[picName];
  });
  
  params.pic_list = picListStr.substr(0, picListStr.length - 1);
  
  return buildContent(params);
}

var buildContent = function (params) {
  var meta = "";
  var body = "";
  var start = 0;
  
  Object.keys(params).forEach(function (key) {
    if (meta.length > 0) {
      meta += "#";
    }
    meta += key +"," + start + "," + (start + params[key].toString().length);
    body += params[key];
    start += params[key].length;
    })
  
  return meta + "^" + body;
}

client.addItem({
  instanceName: instanceName,
}, buildAddContent()).then(function (value) {
  console.log("Result", JSON.stringify(value));
}).catch(function (err) {
  console.log("Error Message: ", err);
});

Search for images

The following sample code is used to search for images:
var instanceName = "<InstanceName>"; // The name of the purchased Image Search instance, for example, imagesearchtest.
var start = 0; 
var num = 10;
var catId = "0";
// The 1.jpg image in the current directory is used as an example.
var picContent = fs.readFileSync(path.resolve(__dirname, "1.jpg")).toString("base64");

var buildSearchContent = function () {
  var params = {};
  
  params.s = start + "";
  params.n = num + "";
  
  if (!! catId) {
    params.cat_id = catId + "";
  }
  
  var picName = new Buffer("searchPic").toString("base64");
  
  params.pic_list = picName;
  params[picName] = picContent;
  
  return buildContent(params);
}
var buildContent = function (params) {
  var meta = "";
  var body = "";
  var start = 0;
  Object.keys(params).forEach(function (key) {
    
    if (meta.length > 0) {
      meta += "#";
    }
    meta += key +"," + start + "," + (start + params[key].toString().length);
    body += params[key];
    start += params[key].length;
  })
  
  return meta + "^" + body;
}

client.searchItem({
  instanceName: instanceName,
}, buildSearchContent()).then(function (value) {
  console.log("Result", JSON.stringify(value));
}).catch(function (err) {
  console.log("Error Message: ", err);
});

Delete images

The following sample code is used to delete images:
var instanceName = "<InstanceName>"; // The name of the purchased Image Search instance, for example, imagesearchtest.
var itemId = "1234";
var picList = ["1.jpg"]; // If you do not specify this parameter, the system deletes all the images that correspond to the specified itemId parameter.

var buildDeleteContent = function () {
  if (! itemId) {
    return ;
  }
  
  var params = {};
  params.item_id = itemId;
  
  var picListStr = "";
  
  picList.forEach(function (picName) {
    if (picName.length >= 0) {
      picListStr += new Buffer(picName).toString("base64") + ",";
    }
  });
  
  params.pic_list = picListStr.substr(0, picListStr.length - 1);
  
  return buildContent(params);
}
var buildContent = function (params) {
  var meta = "";
  var body = "";
  var start = 0;
  
  Object.keys(params).forEach(function (key) {
    if (meta.length > 0) {
      meta += "#";
    }
    meta += key +"," + start + "," + (start + params[key].toString().length);
    body += params[key];
    start += params[key].length;
  })
  
  return meta + "^" + body;
}

client.deleteItem({
  instanceName: instanceName,
}, buildDeleteContent()).then(function (value) {
  console.log("Result", JSON.stringify(value));
}).catch(function (err) {
  console.log("Error Message: ", err);
});