Example of calling an API operation (SDK)

Updated at:
Copy as MD

This topic provides a complete example that shows you how to use the Python software development kit (SDK) to query information about General-purpose Cloud Parallel File System (CPFS) file systems by calling an OpenAPI operation.

View the API documentation

Before you call an API operation, we recommend that you read the API documentation to learn about the parameters and permissions that are required to call the API operation. For more information, see API overview.

Create a Resource Access Management (RAM) user and grant permissions to the RAM user

Note
  • You can call an API operation using an Alibaba Cloud account, a Resource Access Management (RAM) user, or a RAM role. For more information about the differences between these identities, see Identities, credentials, and authorization. This example uses a RAM user to call the API operation.

  • If you have already created a RAM user and granted permissions to the RAM user, skip this step.

  1. Create a RAM user.

    1. Log on to the RAM console. Go to the Users page and click Create User.

    2. Set Logon Name to cpfs-openapi-operator and set Access Mode 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. Grant authorization.

    1. On the Users page, find the RAM user and click Add Permissions in the Actions column.

    2. In the text box, search for AliyunNAS 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 Control NAS access with RAM policies.

    3. Click Grant permissions to complete the authorization.

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/CPFS) SDK.

Install Python

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

Configure environment variables

To avoid the security risks of hard-coding your AccessKey pair in your code, we recommend that you configure environment variables to manage the AccessKey pair. This topic uses the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables as an example. You can replace the variable names as needed, such as with CPFS_ACCESS_KEY_ID and CPFS_ACCESS_KEY_SECRET.

Important

After you configure the environment variables, you may need to restart your development tools, such as IDEs or services, for the new settings to take effect.

Set an Alibaba Cloud AccessKey in environment variables on Linux and macOS

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 environment variables by using the export command:

Important

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=yourAccessKeyID
  • Configure the AccessKey secret and press Enter.

    # Replace yourAccessKeySecret with your AccessKey secret.
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=yourAccessKeySecret
  • Verify the configuration.

    Run the echo $ALIBABA_CLOUD_ACCESS_KEY_ID command. If the command returns the correct AccessKey ID, the configuration is successful.

Set an Alibaba Cloud AccessKey in environment variables on Windows

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.

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% and echo %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 /M

    The /M parameter 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% and echo %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.

Important

After you configure the environment variables, you may need to restart your development tools, such as integrated development environments (IDEs), or services for the new settings to take effect.

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

The following procedure uses the DescribeFileSystems operation as an example. You can select the required API operations in OpenAPI Portal.

  1. In OpenAPI Explorer, go to the DescribeFileSystems page.

  2. In the navigation pane on the left, 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 code

Run the following command:

python sample.py

Sample command output:

{
  "FileSystems": {
    "FileSystem": [
      {
        "Options": {},
        "GuiInfo": {},
        "MountTargetCountLimit": 1,
        "Description": "",
        "NodeNum": 0,
        "ResourceGroupId": "rg-acfm26gwrru****",
        "MeteredSize": 0,
        "VswIds": {
          "VswId": [
            "vsw-8vbmuylc3di7980tj****"
          ]
        },
        "Capacity": 3600,
        "Version": "2.3.4",
        "Packages": {
          "Package": []
        },
        "Bandwidth": 360,
        "ExpiredTime": "",
        "FileSystemId": "cpfs-09b1ccb80257****",
        "Tags": {
          "Tag": []
        },
        "Status": "Running",
        "AccessPointCount": 0,
        "StorageType": "advance_100",
        "Ldap": {},
        "ZoneId": "cn-zhangjiakou-c",
        "CreateTime": "2024-9-6 15:39:27",
        "SupportedFeatures": {
          "SupportedFeature": []
        },
        "MountTargets": {
          "MountTarget": []
        },
        "AutoSnapshotPolicyId": "",
        "EncryptType": 0,
        "VpcId": "vpc-8vbqmlw7s97hu0q1d****",
        "ChargeType": "PayAsYouGo",
        "ProtocolType": "cpfs",
        "FileSystemType": "cpfs",
        "RegionId": "cn-zhangjiakou"
      }
    ]
  },
  "TotalCount": 1,
  "RequestId": "6E561766-2667-522A-B38A-9AE846F05B1C",
  "PageSize": 10,
  "PageNumber": 1
}