OpenAPI integration example (SDK)
The Alibaba Cloud software development kit (SDK) is a common way to call OpenAPI. This topic uses Python as an example to show you how to use the Alibaba Cloud OpenAPI to query data stream information for a CPFS for Lingjun file system.
View OpenAPI documentation
Before you call an OpenAPI, read the corresponding API documentation to understand the required parameters and permissions. For more information, see API overview.
User identity management
Identity
You can use an Alibaba Cloud account, a Resource Access Management (RAM) user, or a RAM role to call this API. For information about the differences between these identities, see Identities, credentials, and authorization. This example uses a RAM user to call the API. For more information, see Create a RAM user.
Authorization
Go to the RAM User List. In the Actions column of the target RAM user, click Add Permissions.
In the text box, enter the keyword
AliyunNASto search, and then select the AliyunNASReadOnlyAccess policy.NoteAliyunNASFullAccess: Grants permissions to manage Cloud Parallel File Storage (CPFS).
AliyunNASReadOnlyAccess: Grants read-only permissions to access Cloud Parallel File Storage (CPFS).
To create a custom policy, see Authorization information.
Click Confirm Authorization to complete the authorization.
Credentials
Go to the details page of the RAM user. On the Authentication Management tab, click Create AccessKey. 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 for other languages is similar. For more information, see File Storage (NAS/CPFS) SDK.
Prepare the Python environment
Download and install Python 3.
To check your Python version, open a terminal and run the python --version command.
Configure environment variables
To avoid the security risks of hard-coding your AccessKey into your business code, configure environment variables to manage your AccessKey. This topic uses the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET as an example. You can replace the variable names as needed, such as CPFS_ACCESS_KEY_ID and CPFS_ACCESS_KEY_SECRET.
After you configure the environment variables, you may need to restart your development tools, such as IDEs, or services to apply the new settings.
Linux and macOS systems
Configure environment variables using the export command
A temporary environment variable set using the export command is valid only for the current session. The variable is cleared when the session ends. For long-term retention (LTR), add the export command to the startup configuration file of your operating system.
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 system
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.
Using 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
In your terminal, run the following commands one by one to install the dependencies.
pip install alibabacloud_nas20170626==2.0.3
pip3 install alibabacloud_tea_consoleDownload the sample code
The following procedure uses the DescribeDataFlows API as an example. You can select the API you want to call in the OpenAPI Portal.
Go to the DescribeDataFlows page in the OpenAPI Portal.
On the Parameter Configuration tab on the left, enter the required parameter information. In this example, set Endpoint to China (Ulanqab).
On the SDK Sample tab on the right, select Python and click the Download Complete Project button to download the sample code package.
Extract the package locally and go to the alibabacloud_sample folder.
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****"
}
]
}
}