OpenAPI integration example (SDK)
Alibaba Cloud SDK is a common method for calling OpenAPI. This topic uses Python as an example to describe how to use Alibaba Cloud OpenAPI to query data flow information of CPFS for AI file systems.
View OpenAPI documentation
Before calling an OpenAPI operation, we recommend that you read the corresponding API documentation to learn about the required parameters and permissions. For more information, see API overview.
User identity management
Identity
You can use an Alibaba Cloud account (primary account), RAM user, or RAM role to call this API operation. For more information about the differences between identity types, see Identity, credentials, and authorization. In this example, we use a RAM user to call the API operation. For more information, see Create a RAM user.
Authorization
Go to RAM user list, and click Grant Permission in the Actions column of the target RAM user.
In the text box, enter the keyword
AliyunNASto search and select the AliyunNASReadOnlyAccess policy.NoteAliyunNASFullAccess: the permission to manage File Storage CPFS.
AliyunNASReadOnlyAccess: the read-only permission on File Storage CPFS.
To create a custom permission policy, see RAM authorization.
Click key to complete the authorization.
Credential
Go to the details page of the RAM user, click Create an AccessKey on the Authentication tab. For detailed steps, see Create an AccessKey.
Call OpenAPI
This topic uses the Alibaba Cloud SDK for Python to call OpenAPI as an example. The usage of SDKs in other languages is similar. For more information, see File Storage (NAS/CPFS) SDK.
Prepare the Python environment
Download and install Python 3.
To check the Python version, open a terminal and run python --version.
Configure environment variables
To avoid the security risks of hard-coding an AccessKey in your business code, we recommend that you manage AccessKeys by configuring environment variables. This topic uses ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET as examples. You can replace the variable names as needed, such as CPFS_ACCESS_KEY_ID and CPFS_ACCESS_KEY_SECRET.
After the environment variables are configured, you may need to restart your development tools (such as IDEs) or services to ensure the new settings take effect.
Linux and macOS
Configure the AccessKey ID and press Enter.
# Replace yourAccessKeyID with your AccessKey ID. export ALIBABA_CLOUD_ACCESS_KEY_ID=yourAccessKeyIDConfigure the AccessKey secret and press Enter.
# Replace yourAccessKeySecret with your AccessKey secret. export ALIBABA_CLOUD_ACCESS_KEY_SECRET=yourAccessKeySecretVerify the configuration.
Run the
echo $ALIBABA_CLOUD_ACCESS_KEY_IDcommand. If the command returns the correct AccessKey ID, the configuration is successful.
Windows
Use the graphical user interface (GUI)
Procedure
The following steps describe how to set environment variables using the GUI in Windows 10.
On your desktop, right-click This PC and choose Properties > Advanced system settings > Environment Variables > New under System variables or User variables. Then, complete the configuration.
Variable
Example value
AccessKey ID
Variable name: ALIBABA_CLOUD_ACCESS_KEY_ID
Variable value: yourAccessKeyID
AccessKey secret
Variable name: ALIBABA_CLOUD_ACCESS_KEY_SECRET
Variable value: yourAccessKeySecret
Test the configuration
Click Start (or use the Win+R keyboard shortcut), click Run, enter `cmd`, and then click OK (or press Enter) to open the command prompt. Run the
echo %ALIBABA_CLOUD_ACCESS_KEY_ID%andecho %ALIBABA_CLOUD_ACCESS_KEY_SECRET%commands. If the commands return the correct AccessKey, the configuration is successful.
Use the command prompt (CMD)
Procedure
Open the command prompt as an administrator and run the following commands to add new environment variables to the system.
setx ALIBABA_CLOUD_ACCESS_KEY_ID yourAccessKeyID /M setx ALIBABA_CLOUD_ACCESS_KEY_SECRET yourAccessKeySecret /MThe
/Mparameter indicates a system environment variable. You can omit this parameter when you set a user environment variable.Test the configuration
Click Start (or use the Win+R keyboard shortcut), click Run, enter `cmd`, and then click OK (or press Enter) to open the command prompt. Run the
echo %ALIBABA_CLOUD_ACCESS_KEY_ID%andecho %ALIBABA_CLOUD_ACCESS_KEY_SECRET%commands. If the commands return the correct AccessKey, the configuration is successful.
Use Windows PowerShell
In PowerShell, you can set new environment variables that are valid for all new sessions:
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'yourAccessKeyID', [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'yourAccessKeySecret', [System.EnvironmentVariableTarget]::User)To set environment variables for all users, you must have administrative permissions:
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'yourAccessKeyID', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'yourAccessKeySecret', [System.EnvironmentVariableTarget]::Machine)You can set temporary environment variables that are valid only for the current session:
$env:ALIBABA_CLOUD_ACCESS_KEY_ID = "yourAccessKeyID"
$env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "yourAccessKeySecret"In PowerShell, run the Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_ID and Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_SECRET commands. If the commands return the correct AccessKey, the configuration is successful.
Install dependencies
Run the following commands one by one in the terminal to install environment dependencies.
pip install alibabacloud_nas20170626==2.0.3
pip3 install alibabacloud_tea_consoleDownload sample code
The following example calls the DescribeDataFlows API operation. You can select the API operation you want to call in OpenAPI Explorer.
Go to DescribeDataFlows to access the debugging page of this API operation in OpenAPI Explorer.
On the Parameters tab on the left side, enter the required parameter information. In this example, Endpoint is set to China North 6 (Ulanqab).
On the SDK Sample tab on the right side, select the Python language, and click the Download Complete Project button to download the sample code package.
Extract the package locally and navigate to the alibabacloud_sample directory.
Run the code
Run the following command
python sample.pyThe following output is returned:
{
"RequestId": "9899B4E0-CEA0-1937-936D-69023F859C8C",
"NextToken": "",
"DataFlowInfo": {
"DataFlow": [
{
"Status": "Running",
"FileSystemPath": "/stream_test/",
"SourceStorage": "oss://cpfs-data****-wulan-test",
"SourceStoragePath": "/stream_test/",
"CreateTime": "2024-07-03T13:43:08Z",
"UpdateTime": "2024-07-03T13:43:09Z",
"FileSystemId": "bmcpfs-370l0qotjv0lbwe****",
"DataFlowId": "df-374f9fad8c1b****"
}
]
}
}