本文介绍图像搜索服务Node.js SDK的使用方法及示例。

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见创建AccessKey
  • 安装图像搜索Node.js SDK,Node.js大于等于6.X版本。
    $ npm install @alicloud/imagesearch-2018-01-20  --save

创建Client

创建Client代码示例如下。
var Client = require("@alicloud/imagesearch-2018-01-20");
var client = new Client({
  accessKeyId: "<AccessKeyID>",  // 获取地址: https://ak-console.aliyun.com
  accessKeySecret: "<AccessKeySecret>",
  endpoint: "http://imagesearch.ap-southeast-1.aliyuncs.com", // 获取地址: https://www.alibabacloud.com/help/zh/doc-detail/179213.html
  apiVersion: "2018-01-20"
});

新增图片

新增图片时,商品图片搜索实例用户可以选择指定或者不指定类目ID,通用图片搜索实例请不要指定类目ID。新增图片代码示例如下。
var instanceName = "<InstanceName>"; // 购买的图像搜索实例名称,如: imagesearchtest
var catId = "0"; // 类目ID,参考: https://www.alibabacloud.com/help/zh/doc-detail/179184.html
var itemId = "1234"; // 商品/图片ID
var custContent = "{\"key\":\"value\"}"; // 商品/图片的Meta信息,搜索结果中会透传
var picList = {}; // 图片列表

// 下面以当前目录下的1.jpg 和 2.jpg 为例
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);
});

查询图片

查询图片代码示例如下。
var instanceName = "<InstanceName>"; // 购买的图像搜索实例名称,如: imagesearchtest
var start = 0; 
var num = 10;
var catId = "0";
// 下面以当前目录下的 1.jpg 为例
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);
});

删除图片

删除图片代码示例如下。
var instanceName = "<InstanceName>"; // 购买的图像搜索实例名称,如: imagesearchtest
var itemId = "1234";
var picList = ["1.jpg"];  // 如果不传入,则删除itemId下对应所有图片

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);
});