3

我正在为 AWS 开发 Rails 应用程序,并希望为区域名称创建下拉菜单,例如“us-east-1”等。

如果有人已经创建了 gem 来获取它们,我想使用它。有谁知道这样的宝石或有用的 API?

4

3 回答 3

5

适用于 Ruby 的 AWS 开发工具包 (aws-sdk gem) 支持枚举区域名称:

require 'aws-sdk'

ec2 = AWS::EC2.new(:access_key_id => '...', :secret_access_key => '...')
ec2.regions.map(&:name)
=> ["eu-west-1", "sa-east-1", "us-east-1", "ap-northeast-1", "us-west-2", "us-west-1", "ap-southeast-1"] 

您还可以对 DescribeRegions 调用使用客户端接口:

ec2.client.describe_regions
=> { :region_info=>[
    {:region_name=>"eu-west-1", :region_endpoint=>"ec2.eu-west-1.amazonaws.com"}, 
    {:region_name=>"sa-east-1", :region_endpoint=>"ec2.sa-east-1.amazonaws.com"}, 
    {:region_name=>"us-east-1", :region_endpoint=>"ec2.us-east-1.amazonaws.com"}, 
    {:region_name=>"ap-northeast-1", :region_endpoint=>"ec2.ap-northeast-1.amazonaws.com"}, 
    {:region_name=>"us-west-2", :region_endpoint=>"ec2.us-west-2.amazonaws.com"}, 
    {:region_name=>"us-west-1", :region_endpoint=>"ec2.us-west-1.amazonaws.com"}, 
    {:region_name=>"ap-southeast-1", :region_endpoint=>"ec2.ap-southeast-1.amazonaws.com"}
  ], 
  :request_id=>"04458cac-bdf2-4847-bf1f-c7ea65813777"
} 

您可以在此处查看 gem 文档:http: //docs.amazonwebservices.com/AWSRubySDK/latest/frames.html

于 2012-07-26T06:37:39.430 回答
1

在我的身上,我可以做到:

>> `ec2-describe-regions`.scan(/REGION\s+(\S+)/).flatten
=> ["eu-west-1", "sa-east-1", "us-east-1", "ap-northeast-1", "us-west-2", "us-west-1", "ap-southeast-1"]
于 2012-07-26T04:58:21.197 回答
1

我找到了宝石!

http://rubydoc.info/gems/aws/2.5.6/Aws/Ec2:describe_regions

于 2012-07-26T05:45:43.127 回答