Python SDK call example

更新时间:
复制 MD 格式

This topic describes how to use the Alibaba Cloud Simple Log Service Python SDK to query project information by calling the Simple Log Service ListProject API operation.

View the OpenAPI documentation

Before you call an OpenAPI operation, read the documentation for the operation to learn about its parameters and required permissions. For more information, see API overview.

Create a RAM user and grant permissions

Note

The AccessKey pair of an Alibaba Cloud account has the highest permissions. We recommend that you create a RAM user and grant only the required permissions to the RAM user based on the principle of least privilege. This prevents the use of the username and password or AccessKey pair of an Alibaba Cloud account and reduces the security risks of enterprises.

  1. Log on to the RAM console by using your Alibaba Cloud account or a RAM user who has administrative rights.

  2. Create a RAM user.

    1. In the left-side navigation pane, choose Identities > Users.

    2. On the Users page, click Create User.

    3. On the Create User page, specify openapi-operator for the Logon Name and Display Name parameters and set the Access Mode parameter to Using permanent AccessKey to access.

    4. Click OK.

    Important
    • After you create a RAM user, the system automatically generates an AccessKey ID and an AccessKey secret for the RAM user. For more information, see Create an AccessKey pair.

    • You must store the AccessKey pair and keep it in confidential. When you use the RAM user to call an API operation, you must enter the AccessKey pair.

  3. Grant permissions to the RAM user.

    Note

    After you create a RAM user, the RAM user does not have any permissions. We recommend that you grant only the required permissions to the RAM user based on the principle of least privilege. In this example, the AliyunLogReadOnlyAccess policy is attached to the RAM user.

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

    2. In the Grant Permission panel, select the AliyunLogReadOnlyAccess policy and click Grant permissions.

    3. Confirm the authorization result and click Close.

    The following table describes the policies.

    Policy

    Description

    AliyunLogFullAccess

    Management permissions on Simple Log Service resources.

    AliyunLogReadOnlyAccess

    Read-only permissions on Simple Log Service resources.

    Custom policy

    If system policies do not meet your business requirements, you can create a custom policy to implement fine-grained access control. For more information, see Create custom policies.

    For more information about sample policies, see Examples of custom RAM policies and RAM policies.

Call an OpenAPI operation

This topic describes how to use the Alibaba Cloud Python SDK to call an OpenAPI operation. The process is similar for SDKs in other languages. For more information, see Simple Log Service SDK.

Prepare the Python environment

Install Python. For more information, see the official Python website.

Note

The Simple Log Service Python SDK supports Python 3 and later.

Run the python -V command to check the installed Python version.

Configure environment variables

To prevent security risks from hard-coding an AccessKey pair into your code, we recommend that you use environment variables to manage the AccessKey pair.

  • Linux and macOS

    Replace <access_key_id> and <access_key_secret> in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    Create a file for your environment variables. Add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, and set them to the AccessKey ID and AccessKey secret of the RAM user. Then, restart Windows.

Download the sample code

  1. Go to ListProject.

  2. On the Parameter Settings tab, specify the required parameters, such as the projectName that you want to query.

  3. On the SDK Sample Code tab, select Python and click Download Project to download the sample code package.

  4. Decompress the package and navigate to the alibabacloud_sample folder.

Install dependencies

Note

After you download the sample code, refer to the README.md file for information about the SDK version and its dependencies.

pip install alibabacloud-sls20201230

Run the code

Run the sample.py file. The output is similar to the following:

[LOG]
{
  "headers": {
    "server": "AliyunSLS",
    "content-type": "application/json",
    "content-length": "230",
    "connection": "keep-alive",
    "access-control-allow-origin": "*",
    "date": "Tue, 25 Jul 2023 10:08:25 GMT",
    "x-log-time": "1690279705",
    "x-log-requestid": "64BF9F199ED2B749708E5445"
  },
  "statusCode": 200,
  "body": {
    "count": 1,
    "projects": [
      {
        "createTime": "1656666120",
        "description": "",
        "lastModifyTime": "1665978308",
        "owner": "",
        "projectName": "re****-nginx",
        "region": "cn-hangzhou",
        "resourceGroupId": "rg-a****a",
        "status": "Normal"
      }
    ],
    "total": 1
  }
}