Python SDK example

更新时间:
复制 MD 格式

This topic shows you how to use the Alibaba Cloud SDK for Python to call the DescribeDBInstances API and query your ApsaraDB for RDS instances.

Before you begin

Before you call an API, review its documentation to understand the required parameters and permissions. For more information, see API overview.

Create a RAM user and grant permissions

If you already have a Resource Access Management (RAM) user with the required permissions, you can skip this section.

  1. Create a RAM user:

    1. In the RAM console, navigate to the Users page and click Create User.

    2. Set Logon Name to rds-openapi-operator, and set Access Method to Programmatic Access.

    3. Click OK. After the RAM user is created, save the new user's AccessKey ID and AccessKey Secret.

  2. Grant permissions to the RAM user:

    1. On the Users page, find the target RAM user and click Add Permissions in the Actions column.

    2. In the search box, enter AliyunRDS and select the AliyunRDSReadOnlyAccess policy.

      Note

      The AliyunRDSReadOnlyAccess policy grants permissions to query ApsaraDB for RDS instances. You can also select the AliyunRDSFullAccess policy, which grants full permissions to query and modify ApsaraDB for RDS instances, or create a custom policy. For more information, see Create a custom policy.

    3. Click OK.

Call the API

The following steps show how to call an API by using the Alibaba Cloud SDK for Python. The procedure is similar for SDKs in other languages. For more information, see ApsaraDB for RDS SDK.

Prepare the Python environment

Download and install Python 3. If Python is already installed, run the python --version command to check its version.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • On Linux and macOS, replace <access_key_id> and <access_key_secret> with your AccessKey ID and AccessKey Secret. Then, run the following commands in your terminal:

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • On Windows, create the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables and set their values to your AccessKey ID and AccessKey Secret. Then, restart your system for the changes to take effect.

Install dependencies

In your terminal, run the following command to install the required dependency:

pip install alibabacloud_rds20140815==5.0.1

Download the sample code

  1. In OpenAPI Explorer, go to the DescribeDBInstances page.

  2. On the Parameter Configuration tab, configure the required parameters. For this example, set RegionId (region ID) to cn-beijing.

  3. In the right-side panel, on the SDK Sample tab, select Python and click Download Project to save the sample code package.

  4. Extract the downloaded package and navigate to the alibabacloud_sample directory.

Run the code

Run the following command:

python sample.py

The following output is a sample response:

{
    "headers": {
        "date": "Tue, 18 Jun 2024 10:12:14 GMT",
        "content-type": "application/json;charset=utf-8",
        "content-length": "1018",
        "connection": "keep-alive",
        "keep-alive": "timeout=25",
        "access-control-allow-origin": "*",
        "access-control-expose-headers": "*",
        "x-acs-request-id": "3BBF9A9B-E74C-5EF5-99BB-4F65B4B67499",
        "x-acs-trace-id": "dcfce27991f7d63647cc3c31ae3daa65",
        "etag": "1se2pACCKd0kse87Rqtbprg8"
    },
    "statusCode": 200,
    "body": {
        "Items": {
            "DBInstance": [
                {
                    "ConnectionMode": "Standard",
                    "ConnectionString": "rm-2zea24972vgw2****.mysql.rds.aliyuncs.com",
                    "CreateTime": "2024-06-18T10:09:56Z",
                    "DBInstanceClass": "mysql.n1e.small.1",
                    "DBInstanceId": "rm-2zea24972vgw2****",
                    "DBInstanceMemory": 1024,
                    "DBInstanceNetType": "Intranet",
                    "DBInstanceStatus": "Creating",
                    "DBInstanceStorageType": "general_essd",
                    "DBInstanceType": "Primary",
                    "DeletionProtection": false,
                    "Engine": "MySQL",
                    "EngineVersion": "8.0",
                    "ExpireTime": "2024-07-18T16:00:00Z",
                    "InstanceNetworkType": "VPC",
                    "LockMode": "Unlock",
                    "MutriORsignle": false,
                    "PayType": "Prepaid",
                    "ReadOnlyDBInstanceIds": {
                        "ReadOnlyDBInstanceId": []
                    },
                    "RegionId": "cn-beijing",
                    "ResourceGroupId": "rg-acfmz7u4zzr****",
                    "TipsLevel": 0,
                    "VSwitchId": "vsw-2zeyawzswr2lno0ys****",
                    "VpcCloudInstanceId": "rm-2zea24972vgw2****-20240618180950",
                    "VpcId": "vpc-2zeev9fov0chw8hjt****",
                    "ZoneId": "cn-beijing-l"
                }
            ]
        },
        "NextToken": "o7PHCFqQhehg8NUW9EJ7Yw",
        "PageNumber": 1,
        "PageRecordCount": 1,
        "RequestId": "3BBF9A9B-E74C-5EF5-99BB-4F65B4B67499",
        "TotalRecordCount": 1
    }
}