阿里云云产品的API分为RPC和RESTful两种类型,大部分产品使用的是RPC风格。当您使用阿里云CLI调用接口时,不同风格的API,调用方式不同。

在阿里云CLI中,使用的命令行结构如下:
aliyun <command> <subcommand> [options and parameters]
  • aliyun:阿里云工具名。
  • command:指定一个顶级命令。
    1. 通常表示命令行工具中支持的阿里云产品基础服务,例如ecs、rds等。
    2. 也表示命令行工具本身的功能命令,例如helpconfigure等。
  • subcommand:指定要执行操作的附加子命令,即具体的某一项操作。
  • options and parameters:指定用于控制阿里云CLI行为的选项或者API参数选项,其选项值可以是数字、字符串和JSON结构字符串等。
调用产品接口时,首先需要判断API类型,选择标准的命令结构发起调用。您可以通过以下特点判断API类型:
  • API参数中包含Action字段的是RPC API,需要PathPattern参数的是RESTful API。
  • 一般情况下,每个产品内所有API的调用风格是统一的。
  • 每个API仅支持特定的一种风格,传入错误的标识,可能会调用到其他API,或收到ApiNotFound的错误信息。

调用RPC API

在阿里云CLI中,调用RPC API时,基本命令结构如下:

aliyun <product> <ApiName> [--parameter1 value1 --parameter2 value2 ...]

代码示例:

aliyun rds DescribeDBInstances --PageSize 50
aliyun ecs DescribeRegions
aliyun rds DescribeDBInstanceAttribute --DBInstanceId xxxxxx

调用RESTful API

部分阿里云产品如容器服务是RESTful API。在阿里云CLI中,调用RESTful API时,基本命令结构如下:
 aliyun 云产品code [GET|PUT|POST|DELETE] <PathPattern> --body "$(cat input.json)"
  • GET请求

    请求结构:

    aliyun 云产品code GET /资源

    示例命令:

    aliyun cs GET /clusters
  • POST请求

    请求结构:

    aliyun 云产品code POST /资源 --body "$(cat input.json)"

    示例命令:

    aliyun cs  POST /clusters/<cluster_id>/attach --header "Content-Type=application/json" --body "$(cat attach.json)"
  • DELETE请求

    请求结构:

    aliyun 云产品code DELETE /资源

    示例命令:

    aliyun cs DELETE /clusters/ce2cdc26227e09c864d0ca0b2d5*****