Integrate an SDK

更新时间:
复制 MD 格式

Integrate the V1.0 Python SDK into your project to simplify OpenAPI calls, speed up feature integration, and reduce maintenance costs.

Environment requirements

Python >= 3.7

Install the SDK

Installing a product SDK automatically installs the core SDK. You only need to install the product SDK.

Product SDK

A product SDK provides the Request and Response objects for OpenAPI calls. For example, install the ECS SDK:

pip install aliyun-python-sdk-ecs

The naming convention for a V1.0 SDK is aliyun-python-sdk-${product_code}. You can also find V1.0 SDKs for specific products in the SDK Center.

Core SDK

The core SDK provides the Client object, request signing, and exception handling. To make generalized calls or install a specific core SDK version, run:

pip install aliyun-python-sdk-core

View available versions on aliyun-python-sdk-core · PyPI.

Use the SDK

The following example calls the ECS DescribeInstances API with the V1.0 SDK.

1. Initialize the request client

All OpenAPI calls go through the Client object in the core SDK. Initialize the client before calling any API. This example uses an AccessKey pair. For other credential types, see Manage access credentials.

Note

Before running the code, set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. Configure environment variables on Linux, macOS, and Windows.

import os

from aliyunsdkcore.client import AcsClient

# Initialize the client
client = AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),  # Get the AccessKey ID of the RAM user from an environment variable
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # Get the AccessKey secret of the RAM user from an environment variable
    '<REGION_ID>'  # Region ID
)

2. Construct an OpenAPI request parameter object

Use the Request object from the product SDK to set request parameters.

Note

The naming convention for an OpenAPI request parameter object is <OpenAPIName>Request.

from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest

# Construct a DescribeInstancesRequest object
request = DescribeInstancesRequest()
request.set_InstanceIds("[\"i-bp1dXXXXXXXXXXXX\"]")
request.set_PageSize(100)
request.set_PageNumber(1)

3. Send the request

Call do_action_with_exception on the client with the Request object from Step 2. A successful call returns a Response object.

response = client.do_action_with_exception(request)
print(response)

4. Handle exceptions

Catch ClientException and ServerException to handle errors. A ClientException typically indicates incorrect initialization parameters such as an invalid AccessKey. A ServerException has various causes — provide the RequestId from the exception when contacting Alibaba Cloud support.

Complete code example

import os

from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest

try:
    # Initialize the client
    client = AcsClient(
        os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),  # Get the AccessKey ID of the RAM user from an environment variable
        os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # Get the AccessKey secret of the RAM user from an environment variable
        'cn-hangzhou'  # Region ID. cn-hangzhou is used as an example.
    )

    # Construct a DescribeInstancesRequest object
    request = DescribeInstancesRequest()
    request.set_InstanceIds("[\"i-bp1dXXXXXXXXXXXX\"]")
    request.set_PageSize(100)
    request.set_PageNumber(1)

    #  Send the request
    response = client.do_action_with_exception(request)
    print(response)
except ClientException as e:
    # This is for printing and demonstration purposes only. Handle exceptions with care. Do not ignore exceptions in your projects.
    print(e)
except ServerException as e:
    # This is for printing and demonstration purposes only. Handle exceptions with care. Do not ignore exceptions in your projects.
    print(e)

Configure a pip mirror source in the Chinese mainland (Optional)

Use an Alibaba Cloud mirror to speed up pip downloads in the Chinese mainland.

Temporary Use

Append -i with the mirror URL to a pip command for one-time use. Example with aliyun-python-sdk-core:

 pip install aliyun-python-sdk-core -i https://mirrors.aliyun.com/pypi/simple/ 

Permanent Configuration

To set a permanent mirror, edit the pip configuration file: ~/.pip/pip.conf on Linux/macOS or %USERPROFILE%\pip\pip.ini on Windows. Create the file if it does not exist.

Linux/macOS

  1. Open or create the configuration file:

    mkdir -p ~/.pip && vim ~/.pip/pip.conf
    
  2. Press i to enter edit mode, then add the following content:

    [global]
    index-url = http://mirrors.cloud.aliyuncs.com/pypi/simple/
    
  3. Press ESC, type :wq, and press Enter to save. Verify the configuration:

    pip config list
    
    # If the following information is displayed, the configuration is complete.
    global.index-url='http://mirrors.cloud.aliyuncs.com/pypi/simple/'
    

Windows

Create a pip.ini file in the %USERPROFILE%\pip folder with the following content:

[global]
index-url = http://mirrors.cloud.aliyuncs.com/pypi/simple/

References