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
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.
Log on to the RAM console by using your Alibaba Cloud account or a RAM user who has administrative rights.
Create a RAM user.
In the left-side navigation pane, choose .
On the Users page, click Create User.
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.
Click OK.
ImportantAfter 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.
Grant permissions to the RAM user.
NoteAfter 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.
On the Users page, find the openapi-operator RAM user and click Add Permissions in the Actions column.
In the Grant Permission panel, select the AliyunLogReadOnlyAccess policy and click Grant permissions.
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.
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_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables, and set them to the AccessKey ID and AccessKey secret of the RAM user. Then, restart Windows.
Download the sample code
Go to ListProject.
On the Parameter Settings tab, specify the required parameters, such as the projectName that you want to query.
On the SDK Sample Code tab, select Python and click Download Project to download the sample code package.
Decompress the package and navigate to the alibabacloud_sample folder.
Install dependencies
After you download the sample code, refer to the README.md file for information about the SDK version and its dependencies.
pip install alibabacloud-sls20201230Run 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
}
}