Query the details of a sample image in a similar-image library

更新时间:
复制 MD 格式

This topic describes how to use the Go SDK to query the details of a sample image in a similar-image library.

Description

You can call this operation to query the details of a sample image in a similar-image library. For more information about the parameters, see Query the details of a sample image library.

You must use the Content Moderation endpoint to call the service using the SDK. For more information about API endpoints, see Endpoints.

Prerequisites

Go dependencies are installed. For more information, see Installation.

Note

You must use the required Go version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Query the details of a sample image

package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   /**
    * Note: To improve detection performance, reuse the client instance. This avoids repeatedly establishing connections.
    * Common methods to obtain environment variables:
    *     Obtain the AccessKey ID of a Resource Access Management (RAM) user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    *     Obtain the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    */
   client, _err := green.NewClientWithAccessKey(
        "cn-shanghai", 
        "Obtain the AccessKey ID from an environment variable.", 
        "Obtain the AccessKey secret from an environment variable.")
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }

   content, _ := json.Marshal(
      map[string]interface{}{
         "library": "The name of the similar-image library", "dataId": "The ID of the sample image",
      },
   )

   request := green.CreateGetSimilarityImageRequest()
   request.SetContent(content)
   response, _err := client.GetSimilarityImage(request)
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }
   if response.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
   }
   fmt.Println(response.GetHttpContentString())
}