このトピックでは、Alibaba Cloud Image Search SDKs for Node.js の使用方法について説明します。

準備

  • Alibaba Cloud SDK をインストールして使用する前に、Alibaba Cloud アカウントが作成済みであり、AccessKey ペアを取得済みである必要があります。 詳細については、「AccessKey ペアの作成」をご参照ください。
  • Alibaba Cloud Image Search SDK for Node.js をインストールします。 Node.js のバージョンは 6.X 以降である必要があります。
    $ npm install @alicloud/imagesearch-2018-01-20  --save

クライアントの作成

次のコード例は、クライアントの作成に使用されます。
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"
});

画像の追加

商品画像を検索する場合、カテゴリ ID を指定することも指定しないこともできます。 一般的な画像を検索する場合、カテゴリ ID を指定する必要はありません。 次のコード例は、画像を追加するために使用されます。
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);
});

画像の検索

次のコード例は、画像の検索に使用されます。
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);
});

画像の削除

次のコード例は、画像を削除するために使用されます。
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);
});