阿里云首页 阿里云SDK

OpenAPI请求

新版SDK OpenAPI请求

新版 SDK 中每个产品的 Client 包含了所有的 OpenAPI,所以可以指定 OpenAPI 请求方法来请求,其方法名对应 OpenAPI 首字母小写,一般有三个:

可以不用传入运行参数的请求方法

from alibabacloud_ecs20140526.models import DescribeImagesRequest
from alibabacloud_ecs20140526.client import Client
from alibabacloud_tea_openapi.models import Config

'''云服务器示例'''
# 初始化Config
config = Config(
    access_key_id='<ACCESS-KEY-ID>',
    access_key_secret='<ACCESS-KEY-SECRET>',
    region_id='cn-hangzhou'
)
client = Client(config)
# 初始化Request
request = DescribeImagesRequest(image_id='<image-id>')
# 调用api
response = client.describe_images(request)

需要传入运行参数的请求方法

from alibabacloud_ecs20140526.models import DescribeImagesRequest
from alibabacloud_ecs20140526.client import Client
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions

'''云服务器示例'''
# 初始化Config
config = Config(
    access_key_id='<ACCESS-KEY-ID>',
    access_key_secret='<ACCESS-KEY-SECRET>',
    region_id='cn-hangzhou'
)
client = Client(config)
# 初始化Request
request = DescribeImagesRequest(image_id='<image-id>')
# 调用api
response = client.describe_images_with_options(request, RuntimeOptions())

需要进行文件上传的请求方法,该方法默认需要传入运行时参数,这种方法只有需要用到文件上传的 OpenAPI 才会具备

from alibabacloud_imagesearch20200212.client import Client
from alibabacloud_imagesearch20200212.models import SearchImageByPicAdvanceRequest
from alibabacloud_oss_util.models import RuntimeOptions
from alibabacloud_tea_openapi.models import Config

'''图像搜索示例'''

with open('pic.jpg', 'rb') as f:
    # 初始化Request
    request = SearchImageByPicAdvanceRequest(
        instance_name='name',
        pic_content_object=f  # 文件流、BytesIO或者一个类文件对象
    )
    
    # 初始化Config
    config = Config(
        access_key_id='<your_accsee_key_id>',
        access_key_secret='<your_access_key_secret>',
        endpoint='<your_endpoint>',
        region_id='cn-shanghai',
        type='access_key'
    )
    
    # 初始化RuntimeObject
    runtime_option = RuntimeOptions()
    
    # 初始化Client
    client = Client(config)
    
    # 调用api
    response = client.search_image_by_pic_advance(request, runtime_option)