All Products
Search
Document Center

Instructions

Last Updated: Feb 20, 2019
  • Get Your AK (AccessKey ID/AccessKey Secret): Link
  • Get Your Endpoint: Link

Dependency

Node.js >= 6.x

Installation

  1. $ npm install @alicloud/imagesearch-2018-01-20 --save

Create a client

  1. var Client = require("@alicloud/imagesearch-2018-01-20");
  2. var client = new Client({
  3. accessKeyId: "<AccessKeyID>", // Address: https://ak-console.aliyun.com
  4. accessKeySecret: "<AccessKeySecret>",
  5. endpoint: "http://imagesearch.ap-southeast-1.aliyuncs.com", // Address: https://www.alibabacloud.com/help/doc-detail/66616.htm
  6. apiVersion: "2018-01-20"
  7. });

Run API

Add images

For product search instances, category IDs are not necessary. For generic search instances, no category ID is required.

  1. var instanceName = "<InstanceName>"; // Purchased Instance Name,e.g.imagesearchtest
  2. var catId = "0"; // Category ID,Address: https://help.aliyun.com/document_detail/66623.html
  3. var itemId = "1234"; // ID of the product or image.
  4. var custContent = "{\"key\":\"value\"}"; // Custom content.
  5. var picList = {};
  6. // Example: Add 1.jpg and 2.jpg
  7. picList[new Buffer("1.jpg").toString("base64")] = fs.readFileSync(path.resolve(__dirname, "1.jpg")).toString("base64");
  8. picList[new Buffer("2.jpg").toString("base64")] = fs.readFileSync(path.resolve(__dirname, "2.jpg")).toString("base64");
  9. var buildAddContent = function () {
  10. if (!itemId || !catId || Object.keys(picList).length <= 0) {
  11. return ;
  12. }
  13. if (custContent == null) {
  14. custContent = "";
  15. }
  16. var params = {};
  17. params.item_id = itemId;
  18. params.cat_id = catId + "";
  19. params.cust_content = custContent;
  20. var picListStr = "";
  21. Object.keys(picList).forEach(function (picName) {
  22. if (picList[picName].length <= 0) {
  23. return;
  24. }
  25. picListStr += picName + ",";
  26. params[picName] = picList[picName];
  27. });
  28. params.pic_list = picListStr.substr(0, picListStr.length - 1);
  29. return buildContent(params);
  30. }
  31. var buildContent = function (params) {
  32. var meta = "";
  33. var body = "";
  34. var start = 0;
  35. Object.keys(params).forEach(function (key) {
  36. if (meta.length > 0) {
  37. meta += "#";
  38. }
  39. meta += key +"," + start + "," + (start + params[key].toString().length);
  40. body += params[key];
  41. start += params[key].length;
  42. })
  43. return meta + "^" + body;
  44. }
  45. client.addItem({
  46. instanceName: instanceName,
  47. }, buildAddContent()).then(function (value) {
  48. console.log("Result", JSON.stringify(value));
  49. }).catch(function (err) {
  50. console.log("Error Message: ", err);
  51. });

Search images

When you use product search, you can specify a category ID. The system will perform a lookup only on products that belong to the specified category. If you do not specify a category ID, the system will determine the category for you. For generic search, no category ID is required.

  1. var instanceName = "<InstanceName>"; // Purchased Instance Name,e.g.imagesearchtest
  2. var start = 0;
  3. var num = 10;
  4. var catId = "0";
  5. // Example: Search 1.jpg
  6. var picContent = fs.readFileSync(path.resolve(__dirname, "1.jpg")).toString("base64");
  7. var buildSearchContent = function () {
  8. var params = {};
  9. params.s = start + "";
  10. params.n = num + "";
  11. if (!!catId) {
  12. params.cat_id = catId + "";
  13. }
  14. var picName = new Buffer("searchPic").toString("base64");
  15. params.pic_list = picName;
  16. params[picName] = picContent;
  17. return buildContent(params);
  18. }
  19. var buildContent = function (params) {
  20. var meta = "";
  21. var body = "";
  22. var start = 0;
  23. Object.keys(params).forEach(function (key) {
  24. if (meta.length > 0) {
  25. meta += "#";
  26. }
  27. meta += key +"," + start + "," + (start + params[key].toString().length);
  28. body += params[key];
  29. start += params[key].length;
  30. })
  31. return meta + "^" + body;
  32. }
  33. client.searchItem({
  34. instanceName: instanceName,
  35. }, buildSearchContent()).then(function (value) {
  36. console.log("Result", JSON.stringify(value));
  37. }).catch(function (err) {
  38. console.log("Error Message: ", err);
  39. });

Delete images

  1. var instanceName = "<InstanceName>"; // Purchased Instance Name,e.g.imagesearchtestvar itemId = "1234";
  2. var itemId = "1234";
  3. var picList = ["1.jpg"]; // If not set, it will delete all images belong to item
  4. var buildDeleteContent = function () {
  5. if (!itemId) {
  6. return ;
  7. }
  8. var params = {};
  9. params.item_id = itemId;
  10. var picListStr = "";
  11. picList.forEach(function (picName) {
  12. if (picName.length >= 0) {
  13. picListStr += new Buffer(picName).toString("base64") + ",";
  14. }
  15. });
  16. params.pic_list = picListStr.substr(0, picListStr.length - 1);
  17. return buildContent(params);
  18. }
  19. var buildContent = function (params) {
  20. var meta = "";
  21. var body = "";
  22. var start = 0;
  23. Object.keys(params).forEach(function (key) {
  24. if (meta.length > 0) {
  25. meta += "#";
  26. }
  27. meta += key +"," + start + "," + (start + params[key].toString().length);
  28. body += params[key];
  29. start += params[key].length;
  30. })
  31. return meta + "^" + body;
  32. }
  33. client.deleteItem({
  34. instanceName: instanceName,
  35. }, buildDeleteContent()).then(function (value) {
  36. console.log("Result", JSON.stringify(value));
  37. }).catch(function (err) {
  38. console.log("Error Message: ", err);
  39. });