Python SDK sample code
This topic provides a complete code sample that shows how to use the OpenAPI to query file system information in Python.
View the OpenAPI documentation
Read the API overview to learn that the DescribeFileSystems operation is used to query file system information. Refer to the documentation to understand the required data and permissions for calling this operation.
Create a RAM user and grant permissions
If you have already created a Resource Access Management (RAM) user and granted the required permissions, you can skip this step.
Create a RAM user.
Go to User List and click Create user.
Set Logon name to nas-openapi-operator, and select Access Mode as Use a permanent AccessKey for access.
Click OK to create a RAM user and save the AccessKey ID and AccessKey Secret.
Grant permissions.
Go to User List and click Actions in the Add permissions column of the target RAM user.
In the text box, search for
AliyunNASand select the AliyunNASReadOnlyAccess policy.ImportantThe AliyunNASFullAccess policy grants full operational permissions. You can also select the AliyunNASReadOnlyAccess policy, which grants read-only access and viewing permissions, or create a custom policy as needed. For more information, see Control NAS access with RAM policies.
Click OK to complete the authorization.
Call the OpenAPI
This topic uses the Alibaba Cloud software development kit (SDK) for Python to call the OpenAPI. The usage of SDKs for other languages is similar. For more information, see the Apsara File Storage NAS/CPFS SDK.
Prepare Python environment
Download and install Python 3. If you already have Python installed, you can run the following command to check its version: python --version.
Configure environment variables
To avoid security risks from hard coding your AccessKey into your business code, you can configure environment variables to manage your AccessKey. This topic shows how to set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. You can replace the variable names as needed, such as with NAS_ACCESS_KEY_ID and NAS_ACCESS_KEY_SECRET.
After you configure the environment variables, you may need to restart your development tools, such as an Integrated Development Environment (IDE), or related services for the new settings to take effect.
Linux and macOS systems
Configure environment variables by 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.
This section uses the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET as examples. You can replace the variable names as needed, for example, OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
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.
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
On 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
Go to DescribeFileSystems in OpenAPI Portal.
On the Parameter configuration tab on the left, fill in the required parameter information. In this example, Endpoint is set to China (Hangzhou).
On the SDK sample tab on the right, select the Python language, click the Download the complete project button, and download the sample code package.
Decompress the package on your computer and go to the alibabacloud_sample folder.
Run the code
Run the following command:
python sample.pyThe following example shows a sample response:
{
"headers":{
"date":"Wed, 26 Jul 2023 09:03:41 GMT",
"content-type":"application/json;charset=utf-8",
"content-length":"1033",
"connection":"keep-alive",
"keep-alive":"timeout=25",
"vary":"Accept-Encoding",
"access-control-allow-origin":"*",
"access-control-expose-headers":"*",
"x-acs-request-id":"31BF0F2B-9C5F-5FD3-88FA-60621657****",
"x-acs-trace-id":"5842784c3ae9fe9bdcb255bfaa61****",
"etag":"14PmeGvuG5exKT8M004b****"
},
"body":{
"PageNumber":1,
"RequestId":"31BF0F2B-9C5F-5FD3-88FA-60621657****",
"PageSize":10,
"TotalCount":1,
"FileSystems":{
"FileSystem":[
{
"Status":"Running",
"Capacity":10485760,
"MeteredIASize":0,
"CreateTime":"2023-07-24T11:36:17CST",
"ChargeType":"PayAsYouGo",
"StorageType":"Capacity",
"RegionId":"cn-hangzhou",
"FileSystemType":"standard",
"FileSystemId":"197df64****",
"MeteredSize":10625024,
"EncryptType":0,
"Description":"test",
"ExpiredTime":"",
"ZoneId":"cn-hangzhou-g",
"ProtocolType":"NFS",
"KMSKeyId":"",
"MountTargets":{
"MountTarget":[
{
"VpcId":"vpc-2vc1h319fodms9p76****",
"Status":"Active",
"MountTargetDomain":"197df64****-j****.cn-hangzhou.nas.aliyuncs.com",
"AccessGroupName":"DEFAULT_VPC_GROUP_NAME",
"VswId":"vsw-2vcapqvrrxjn15n0m****",
"NetworkType":"vpc",
"ClientMasterNodes":{
"ClientMasterNode":[
]
},
"Tags":{
"Tag":[
]
}
}
]
},
"Packages":{
"Package":[
]
},
"SupportedFeatures":{
"SupportedFeature":[
"Lifecycle",
"Quota",
"Acl"
]
},
"Ldap":{
}
}
]
}
}
}