本文介紹Image Search服務Node.js SDK的使用方法及樣本。

準備工作

安裝Image SearchNode.js SDK。
npm install  @alicloud/imagesearch@1.0.2

Add介面

  • 程式碼範例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXXXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const addImageAdvanceRequest = new imagesearch.AddImageAdvanceRequest({
        // 必填,Image Search執行個體名稱。
        instanceName: "XXXXXXXXXX",
         // 必填,商品id,最多支援 512個字元。
         // 一個商品可有多張圖片。
        productId: "123455",
          // 必填,圖片名稱,最多支援 512個字元。
          // 1. ProductId + PicName唯一確定一張圖片。
          // 2. 如果多次添加圖片具有相同的ProductId + PicName,以最後一次添加為準,前面添加的圖片將被覆蓋。
        picName: "this",
        // 圖片內容,最多支援 2MB大小圖片以及5s的傳輸等待時間。當前僅支援jpg和png格式圖片;
        // 對於商品、通用圖片搜尋,圖片長和寬的像素必須都大於等於200,並且小於等於1024;
        // 映像中不能帶有旋轉資訊
        picContentObject: picContent
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const addResponse = await client.addImageAdvance(addImageAdvanceRequest, ossRuntime);
      console.log(1, addResponse);
    }
    demo();
  • 結果樣本
    AddImageResponse {
      requestId: 'B85D5B4A-92AF-44C1-A3FB-9B554950F8D1',
      success: true,
      message: 'success',
      code: 0,
      picInfo: AddImageResponsePicInfo { categoryId: 0, region: '167,477,221,407' }
    }

SearchImageByPic介面

  • 程式碼範例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXX",
      accessKeySecret: "XXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const searchImageByPicAdvanceRequest = new imagesearch.SearchImageByPicAdvanceRequest({
         // 必填,Image Search執行個體名稱。
        instanceName: "XXXXXXXX",
             // 圖片內容,最多支援 2MB大小圖片以及5s的傳輸等待時間。當前僅支援jpg和png格式圖片;
        // 對於商品、通用圖片搜尋,圖片長和寬的像素必須都大於等於200,並且小於等於1024;
        // 映像中不能帶有旋轉資訊
        picContentObject: picContent
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const searchImageByPicResponse = await client.searchImageByPicAdvance(searchImageByPicAdvanceRequest, ossRuntime);
      console.log(3, searchImageByPicResponse);
    }
    demo();
  • 結果樣本
    SearchImageByPicResponse {
      requestId: 'A38BFBD4-D7AD-4BE9-9606-72BB765A9001',
      success: true,
      code: 0,
      msg: 'success',
      auctions: [
        SearchImageByPicResponseAuctions {
          categoryId: 0,
          productId: '1000',
          picName: 'vae.jpg',
          sortExprValues: '7.33136443711219e+24;0'
        },
      ...............
        SearchImageByPicResponseAuctions {
          categoryId: 0,
          productId: '123455',
          picName: 'this',
          sortExprValues: '7.33136443711219e+24;0'
        }
      ],
      head: SearchImageByPicResponseHead {
        docsReturn: 5,
        docsFound: 5,
        searchTime: 218
      },
      picInfo: SearchImageByPicResponsePicInfo {
        categoryId: 0,
        region: '167,477,221,407',
        allCategories: [
          [SearchImageByPicResponsePicInfoAllCategories],
          ........
          [SearchImageByPicResponsePicInfoAllCategories]
        ]
      }
    }

SearchImageByName介面

  • 程式碼範例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const searchImageByNameRequest = new imagesearch.SearchImageByNameRequest({
         // 必填,Image Search執行個體名稱。
        instanceName: "XXXXXXX",
        // 必填,商品id,最多支援 512個字元。
        // 一個商品可有多張圖片。
        productId: "123455",
        // 必填,圖片名稱,最多支援 512個字元。
       // 1. ProductId + PicName唯一確定一張圖片。
        picName: "this"
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const searchImageByNameResponse = await client.searchImageByName(searchImageByNameRequest, ossRuntime);
      console.log(2, searchImageByNameResponse);
    }
    demo();
  • 結果樣本
    SearchImageByNameResponse {
      requestId: '4B039098-B274-41DE-9A55-32550B6F74E1',
      success: true,
      code: 0,
      msg: 'success',
      auctions: [
        SearchImageByNameResponseAuctions {
          categoryId: 0,
          productId: '1000',
          picName: 'vae.jpg',
          sortExprValues: '7.33136443711219e+24;0'
        },
          .........
        SearchImageByNameResponseAuctions {
          categoryId: 0,
          productId: '123455',
          picName: 'this',
          sortExprValues: '7.33136443711219e+24;0'
        }
      ],
      head: SearchImageByNameResponseHead {
        docsReturn: 5,
        docsFound: 5,
        searchTime: 11
      },
      picInfo: SearchImageByNameResponsePicInfo {
        categoryId: 0,
          allCategories: [
          [SearchImageByNameResponsePicInfoAllCategories],
          ......
          [SearchImageByNameResponsePicInfoAllCategories]
        ]
      }
    }

Delete介面

  • 程式碼範例
    const oss = require('@alicloud/oss-util');
    const fs = require('fs');
    const imagesearch = require('@alicloud/imagesearch')
    const config = new imagesearch.Config({
      accessKeyId: "XXXXXXXXXX",
      accessKeySecret: "XXXXXXXXXXXXX",
      type: "access_key",
      endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
      regionId: "cn-shanghai",
      protocol: 'http'
    });
    const client = new imagesearch.default(config);
    var picContent = fs.createReadStream("D:/123.jpg");
    async function demo() {
      const deleteImageRequest = new imagesearch.DeleteImageRequest({
        // 必填,Image Search執行個體名稱。
        instanceName: "XXXXXXXXXXXXXXXXXX",
         // 必填,商品id,最多支援 512個字元。
         //一個商品可有多張圖片。
        productId: "123455",
         //  選填,圖片名稱。若不指定本參數,則刪除ProductId下所有圖片;若指定本參數,則刪除ProductId+PicName指定的圖片。
        picName: "this"
      });
      let ossRuntime = new oss.RuntimeOptions({});
      const deleteImageResponse = await client.deleteImage(deleteImageRequest, ossRuntime);
      console.log(4, deleteImageResponse);
    }
    demo();
  • 結果樣本
    DeleteImageResponse {
      requestId: '3965B44B-0584-41DE-8764-0D8B36857CE1',
      success: true,
      message: 'success',
      code: 0
    }