All Products
Search
Document Center

Python 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 Python SDK.

    • (Recommended) Install the SDK by using the dependency tool.
      To install the Alibaba Cloud Python SDK, run the following command:

      1. pip install aliyun-python-sdk-core
    • Download and install the SDK on your own.
      You can use the git clone command or use other methods to download aliyun-net-sdk-core and add a solution. To download aliyun-python-sdk-core in GitHub, visit aliyun-python-sdk-core.

  3. Install the Image Search Python SDK.

    • (Recommended) Install the SDK by using the dependency tool.
      To install the Image Search Python SDK, run the following command:

      1. pip install aliyun-python-sdk-imagesearch
    • Download and install the SDK on your own.
      You can run the git clone command or use other methods to download aliyun-python-sdk-imagesearch and add a solution. To download aliyun-python-sdk-imagesearch in GitHub, visit aliyun-python-sdk-imagesearch.

Sample code

  1. # -*- coding: utf8 -*-
  2. from aliyunsdkcore.client import AcsClient
  3. import base64
  4. import aliyunsdkimagesearch.request.v20190325. AddImageRequest as AddImageRequest
  5. import aliyunsdkimagesearch.request.v20190325. DeleteImageRequest as DeleteImageRequest
  6. import aliyunsdkimagesearch.request.v20190325. SearchImageRequest as SearchImageRequest
  7. # Create an AcsClient instance
  8. client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "<region>")
  9. # Add an image
  10. request = AddImageRequest.AddImageRequest()
  11. request.set_endpoint("imagesearch.<region>.aliyuncs.com")
  12. request.set_InstanceName("demo")
  13. request.set_ProductId("test")
  14. request.set_PicName("test")
  15. with open('/home/admin/demo.jpg', 'rb') as imgfile:
  16. encoded_pic_content = base64.b64encode(imgfile.read())
  17. request.set_PicContent(encoded_pic_content)
  18. response = client.do_action_with_exception(request)
  19. print(response)
  20. # Search for an image
  21. request = SearchImageRequest.SearchImageRequest()
  22. request.set_endpoint("imagesearch.<region>.aliyuncs.com")
  23. request.set_InstanceName("demo")
  24. with open('/home/admin/demo.jpg', 'rb') as imgfile:
  25. encoded_pic_content = base64.b64encode(imgfile.read())
  26. request.set_PicContent(encoded_pic_content)
  27. response = client.do_action_with_exception(request)
  28. print(response)
  29. # Delete an image
  30. request = DeleteImageRequest.DeleteImageRequest()
  31. request.set_endpoint("imagesearch.<region>.aliyuncs.com")
  32. request.set_InstanceName("demo")
  33. request.set_ProductId("test")
  34. response = client.do_action_with_exception(request)
  35. print(response)