All Products
Search
Document Center

File Storage NAS:Example of calling an API operation by using NAS SDK for Python

Last Updated:Mar 27, 2025

This topic describes how to use Python to call an API operation to query file system information.

View the API documentation

List of operations by function specifies that the API operation used to query file system information is DescribeFileSystems. Read the documentation to get familiar with the data and permissions required to call this operation.

Create a RAM user and grant permissions to the RAM user

Note

If you have created a Resource Access Management (RAM) user and granted permissions to the RAM user, skip this step.

  1. Create a RAM user.

    1. Go to the Users page and click Create User.

    2. Set Logon Name to nas-openapi-operator and Access Method to Using permanent AccessKey to access.

    3. Click OK. On the page that appears, save the AccessKey ID and AccessKey secret of the RAM user that you created.

  2. Complete authorization.

    1. On the Users page, find the RAM user whose permissions you want to manage and click Add Permissions in the Actions column.

    2. Search for AliyunNAS in the text box and select the AliyunNASReadOnlyAccess policy.

      Important

      The AliyunNASFullAccess policy has the permissions to perform operations. You can also select the AliyunNASReadOnlyAccess policy (which has the access and viewing permissions) or create custom policies. For more information, see Perform access control based on RAM policies.

    3. Click OK.

Call an API operation

This topic describes how to use the Alibaba Cloud SDK for Python to call API operations. The SDKs for other languages can also be used to call API operations in a similar way. For more information, see File Storage NAS SDKs.

Install Python

Download and install Python 3. If you have installed Python, you can run the python --version command to view the Python version.

Configure environment variables

In this example, environment variables are configured to manage the AccessKey pair. This prevents security risks that are caused by hard coding of the AccessKey pair into your business code. In this example, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. You can replace the variable names based on your business requirements, such as NAS_ACCESS_KEY_ID and NAS_ACCESS_KEY_SECRET.

Important

After you configure the environment variables, restart your development tools or services, such as the integrated development environment (IDE). Otherwise, the new settings may not take effect as expected.

Linux and macOS

Configure environment variables by using the export command

Important

The temporary environment variables configured by using the export command are valid only for the current session. After you exit the session, the configured environment variables become invalid. To configure permanent environment variables, you can add the export command to the startup configuration file of the corresponding operating system.

  • Configure the AccessKey ID and press Enter.

    # Replace <ACCESS_KEY_ID> with your AccessKey ID. 
    export ALIBABA_CLOUD_ACCESS_KEY_ID=yourAccessKeyID
  • Configure the AccessKey secret and press Enter.

    # Replace <ACCESS_KEY_SECRET> with your AccessKey secret. 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=yourAccessKeySecret
  • Check whether the configuration is successful.

    Run the echo $ALIBABA_CLOUD_ACCESS_KEY_ID command. If the valid AccessKey ID is returned, the environment variables are configured.

Windows

Use GUI

  • Procedure

    If you want to use GUI to configure environment variables in Windows 10, perform the following steps:

    On the Windows desktop, right-click This PC and select Properties. On the page that appears, click Advanced system settings. In the System Properties dialog box, click Environment Variables on the Advanced tab. In the Environment Variables dialog box, click New in the User variables or System variables section. Then, configure the variables described in the following table.

    Variable

    Example

    AccessKey ID

    • Variable name: ALIBABA_CLOUD_ACCESS_KEY_ID

    • Variable value: yourAccessKeyID

    AccessKey Secret

    • Variable name: ALIBABA_CLOUD_ACCESS_KEY_SECRET

    • Variable value: yourAccessKeySecret

  • Check whether the configuration is successful.

    On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press Enter. On the page that appears, run the echo %ALIBABA_CLOUD_ACCESS_KEY_ID% and echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET% commands. If the valid AccessKey pair is returned, the configuration is successful.

Use CMD

  • Procedure

    Open a Command Prompt window as an administrator and run the following commands to add environment variables in the operating system:

    setx ALIBABA_CLOUD_ACCESS_KEY_ID yourAccessKeyID /M
    setx ALIBABA_CLOUD_ACCESS_KEY_SECRET yourAccessKeySecret /M

    /M indicates that the environment variable is of system level. You can choose not to use this parameter when you configure a user-level environment variable.

  • Check whether the configuration is successful.

    On the Windows desktop, click Start or press Win + R. In the Run dialog box, enter cmd. Then, click OK or press Enter. On the page that appears, run the echo %ALIBABA_CLOUD_ACCESS_KEY_ID% and echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET% commands. If the valid AccessKey pair is returned, the configuration is successful.

Use Windows PowerShell

In PowerShell, configure new environment variables. The environment variables apply to 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)

Configure environment variables for all users. You must run the following commands as an administrator.

[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'yourAccessKeyID', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'yourAccessKeySecret', [System.EnvironmentVariableTarget]::Machine)

Configure temporary environment variables. The environment variables apply only to 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 valid AccessKey pair is returned, the configuration is successful.

Install dependencies

Run the following commands on your terminal to install environment dependencies.

pip install alibabacloud_nas20170626==2.0.3
pip3 install alibabacloud_tea_console

Download the sample code

  1. Go to DescribeFileSystems in OpenAPI Explorer where you can debug APIs.

  2. In the left-side navigation pane, click the Parameters tab and configure the required parameters. In this example, set Region to China (Hangzhou).

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

  4. Decompress the package to an on-premises directory and go to the alibabacloud_sample directory.

Run the sample code

Run the following command:

python sample.py

Sample command output:

{
    "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":{

                    }
                }
            ]
        }
    }
}