All Products
Search
Document Center

Go SDK

Last Updated: Apr 28, 2019

Preparations

  1. Before installing and using Alibaba Cloud SDKs, make sure that you have registered an Alibaba Cloud account and generated an AccessKey. For more information, see Create an AccessKey.

  2. Install the Go SDK. The Alibaba Cloud Go SDK supports Go 1.7 or later. You can install the Go SDK by using the following methods:

    • (Recommended) Use glide.
      Run the following command:

      1. glide get github.com/aliyun/alibaba-cloud-sdk-go
    • Use govendor.
      Run the following command:

      1. go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk

Sample code

  1. package main
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
  6. "github.com/aliyun/alibaba-cloud-sdk-go/services/imagesearch"
  7. "io/ioutil"
  8. )
  9. func main() {
  10. endpoints.AddEndpointMapping("<region>", "ImageSearch", "imagesearch.<region>.aliyuncs.com")
  11. // Create a client instance.
  12. client, err := imagesearch.NewClientWithAccessKey(
  13. "<region>", // Your zone ID
  14. "<your-access-key-id>", // Your AccessKey ID.
  15. "<your-access-key-secret>"); // Your AccessKey Secret.
  16. if err ! = nil {
  17. // Handle exceptions.
  18. panic(err)
  19. }
  20. // Delete an image.
  21. deleteRequest := imagesearch.CreateDeleteImageRequest()
  22. deleteRequest.InstanceName = "demo"
  23. deleteRequest.PicName = "test"
  24. deleteRequest.ProductId = "test"
  25. deleteResponse, err := client.DeleteImage(deleteRequest)
  26. if err ! = nil {
  27. panic(err)
  28. }
  29. fmt.Println(deleteResponse)
  30. // Add an image.
  31. addRequest := imagesearch.CreateAddImageRequest()
  32. addRequest.InstanceName = "demo"
  33. addRequest.PicName = "test"
  34. addRequest.ProductId = "test"
  35. b, err := ioutil.ReadFile("/home/admin/demo.jpg")
  36. if err ! = nil {
  37. panic(err)
  38. }
  39. messageBody := base64. StdEncoding.EncodeToString(b)
  40. addRequest.PicContent = messageBody
  41. addResponse, err := client.AddImage(addRequest)
  42. if err ! = nil {
  43. panic(err)
  44. }
  45. fmt.Println(addResponse)
  46. //Search for an image.
  47. searchRequest := imagesearch.CreateSearchImageRequest()
  48. searchRequest.InstanceName = "demo"
  49. searchRequest.PicName = "test"
  50. searchRequest.ProductId = "test"
  51. searchRequest.Type = "SearchByName"
  52. searchResponse, err := client.SearchImage(searchRequest)
  53. if err ! = nil {
  54. // Handle exceptions.
  55. panic(err)
  56. }
  57. fmt.Println(searchResponse)
  58. }