[Unofficial] Alibaba Cloud SDK for Bash
Go to file
Zhong Lufan df878aea3b
Modify the method to check whether the value is a function
2021-04-18 16:34:28 +08:00
examples Formatted style 2021-04-18 16:31:11 +08:00
.editorconfig Init commit 2019-12-31 18:44:21 +08:00
.gitattributes Update 2019-12-31 23:36:16 +08:00
.gitignore Init commit 2019-12-31 18:44:21 +08:00
AliyunOpenApiSDK.sh Modify the method to check whether the value is a function 2021-04-18 16:34:28 +08:00
LICENSE Init commit 2019-12-31 18:44:21 +08:00
README.md Update README.md 2020-08-08 03:29:29 +08:00

README.md

Aliyun OpenAPI Shell SDK

这是一个非官方的阿里云 OpenAPI Shell SDK方便 Shell 脚本调用阿里云 OpenAPISDK 主要实现了自动计算 OpenAPI 的请求签名。

虽然阿里云官方有 AliyunCLI,可以在 Shell 环境下使用阿里云 OpenAPI不过某些 API (比如 SSL 证书) 它并不支持或者说还没来得及支持而且对于存储空间有限的嵌入式设备Shell SDK 明显是更好的选择。

理论上支持所有阿里云 RPC OpenAPI暂不支持 RESTful OpenAPI将来可能会支持。

这可能是最好用的 Aliyun OpenAPI Shell SDK

依赖

  • bash
  • curl
  • openssl

使用

  1. 导出环境变量 AliAccessKeyIdAliAccessKeySecret
  2. 导入 AliyunOpenApiSDK.sh
  3. 调用 aliapi_rpc 函数

函数签名:

aliapi_rpc(host, http_method, api_version, api_action, api_custom_key[], api_custom_value[]): JsonString

示例:

#!/usr/bin/env bash

# 导出 AliAccessKeyId 和 AliAccessKeySecret
export AliAccessKeyId="<AliAccessKeyId>"
export AliAccessKeySecret="<AliAccessKeySecret>"

# 导入 SDK
source AliyunOpenApiSDK.sh

# 自定义请求参数的键值数组顺序要一一对应,数组成员不能包含空格。
# 自定义值支持自定义函数,如果你需要包含空格或者读取文件等操作,可以声明一个自定义函数,像下面这样。
# 如果自定义值数组成员以 () 结尾SDK 在获取值的时候会判断自定义函数是否存在并执行,如果不存在则使用原始值。

# 自定义请求参数的键
api_custom_key=(
    "CurrentPage"
    "ShowSize"
)
# 自定义请求参数的值
api_custom_value=(
    "1"
    "get_show_size()" # 解析参数时会执行函数 (所以最后提交的值是 50)
)

get_show_size() {
    echo 50
}

# 获取 SSL 证书列表https://help.aliyun.com/document_detail/126511.html
aliapi_rpc "cas.aliyuncs.com" "GET" "2018-07-13" "DescribeUserCertificateList" "${api_custom_key[*]}" "${api_custom_value[*]}"
# $? == 0 代表 HTTP CODE == 200 反之 $? == 1
# 只要 curl 的返回代码 == 0 就会返回接收到的数据
if [[ $? -eq 0 ]]; then
    # 执行成功
else
    # 执行失败
fi

更多示例请参考 example 下的文件

如果你有好的示例,欢迎提交 PR

如果你有建议 / BUG 要反馈,请提交 Issue