Aliyun
The Aliyun ECS API Client for Ruby 是调用 阿里云 ECS服务 的 Ruby客户端类库.
Installation
Add this line to your application's Gemfile:
gem 'aliyun-api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install aliyun-api
Usage
首先,需要在代码中引入类库:
require 'aliyun'
配置aliyun的access_key
1. 全局配置
= {
:access_key_id => "xxxxxx",
:access_key_secret => "yyyyyy"
}
Aliyun.config
ecs = Aliyun::ECS.new
2. 直接配置ECS客户端
= {
:access_key_id => "xxxxxx",
:access_key_secret => "yyyyyy"
}
ecs = Aliyun::ECS.new
3. 环境变量
如果环境变量里ALIYUN_ACCESS_KEY_ID和ALIYUN_ACCESS_KEY_SECRET初始化了密钥,那么可以直接初始化ecs:
ecs = Aliyun::ECS.new
调用ECS
这样, 你就可以根据 阿里云弹性计算服务API参考手册初始化业务参数(除Action参数之外)为一个hash对象,并且将其作为参数传给Action方法(Action参数), action方法需要将阿里云手册中的Action名按ruby方式命名, 如:阿里云手册中的Action名StartInstance对应到这里的方法名为start_instance。
(1) 例如查询可用地域列表,其Action参数为DescribeRegions,而没有其他参数,代码如下
ecs.describe_regions {}
# 输出如下:
{"Regions"=>{"Region"=>[{"LocalName"=>"深圳", "RegionId"=>"cn-shenzhen"}, {"LocalName"=>"青岛", "RegionId"=>"cn-qingdao"}, {"LocalName"=>"北京", "RegionId"=>"cn-beijing"}, {"LocalName"=>"香港", "RegionId"=>"cn-hongkong"}, {"LocalName"=>"杭州", "RegionId"=>"cn-hangzhou"}]}, "RequestId"=>"abcdefg"}
(2) 再比如查询可用镜像,代码如下
parameters = {:RegionId => "cn-beijing", :PageNumber => 2, :RageSize => 20}
service.describe_images parameters
Contributing
- Fork it ( https://github.com/qjpcpu/aliyun-api/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request