aws-sdk-ruby

The official AWS SDK for Ruby.

  • 所有者: aws/aws-sdk-ruby
  • 平台:
  • 许可证: Apache License 2.0
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

AWS SDK for Ruby - Version 3

Gitter Build Status Code Climate Coverage Status
Dependency Status

This is version 3 of the aws-sdk gem. Version 2 can be found at branch:

Change Log

Change Log now can be found at each gem root path, e.g. change log for aws-sdk-s3 gem can be found at /gems/aws-sdk-s3/CHANGELOG.md here. The change log is also accessible via RubyGems.org page under "LINKS" section for changelog.

Installation

The AWS SDK for Ruby is available from RubyGems. aws-sdk gem contains every available AWS service gem support. Please use a major version when expressing a dependency on aws-sdk.

gem 'aws-sdk', '~> 3'

With version 3 modularization, you can also pick the specific AWS service gem to install. Please use a major version when expressing a dependency on service gems.

gem 'aws-sdk-s3', '~> 1'
gem 'aws-sdk-ec2', '~> 1'

Upgrading Guide

Version 3 modularizes the monolithic SDK into service specific gems. Aside from gem packaging differences, version 3 interfaces are backwards compatible with version 2. Following guide contains instructions for both version 1 and version 2 SDK.

Upgrade from version 2

  1. If you depend on aws-sdk or aws-sdk-resources, you don't need to change anything. Meanwhile we recommend you to revisit following options to explore modularization benefits.

  2. If you depend on aws-sdk-core, you must replace this dependency with one of following options. This is because aws-sdk-core now only contains shared utilities.

Options

  1. If you want to keep every AWS service gems in your project, simply keep/switch to aws-sdk
# Gemfile
gem 'aws-sdk', '~> 3'

# or in code
require 'aws-sdk'
  1. If you want to choose several AWS service gems in your project specifically, try following:
# Gemfile
gem 'aws-sdk-s3', '~> 1'
gem 'aws-sdk-ec2', '~> 1'
...

# or in code
require 'aws-sdk-s3'
require 'aws-sdk-ec2'
...

Upgrade from version 1

If you are using SDK version 1 and version 2 together in your application guided by our official blog post, then you might have either aws-sdk ~> 2 or aws-sdk-resources ~> 2 exists in your project, you can simply update it to ~> 3 or using separate service gems as described in version 2 upgrade options.

For additional information of migrating from Version 1 to Version 2, please follow V1 to V2 migration guide.

Additional Information

Getting Help

Please use these community resources for getting help. We use the GitHub issues for tracking bugs and feature requests and have limited bandwidth to address them.

  • Ask a question on StackOverflow and tag it with aws-sdk-ruby
  • Come join the AWS SDK for Ruby Gitter Channel
  • Open a support ticket with AWS Support, if it turns out that you may have found a bug, please open an issue
  • If in doubt as to whether your issue is a question about how to use AWS or a potential SDK issue, feel free to open a GitHub issue on this repo.

Opening Issues

If you encounter a bug with aws-sdk-ruby we would like to hear about it. Search the existing issues and try to make sure your problem doesn’t already exist before opening a new issue. It’s helpful if you include the version of aws-sdk-ruby, ruby version and OS you’re using. Please include a stack trace and reduced repro case when appropriate, too.

The GitHub issues are intended for bug reports and feature requests. For help and questions with using aws-sdk-ruby please make use of the resources listed in the Getting Help section.

Configuration

You will need to configure credentials and a region, either in configuration files or environment variables, to make API calls. It is recommended that you provide these via your environment. This makes it easier to rotate credentials and it keeps your secrets out of source control.

The SDK searches the following locations for credentials:

  • ENV['AWS_ACCESS_KEY_ID'] and ENV['AWS_SECRET_ACCESS_KEY']
  • Unless ENV['AWS_SDK_CONFIG_OPT_OUT'] is set, the shared configuration files (~/.aws/credentials and ~/.aws/config) will be checked for a role_arn and source_profile, which if present will be used to attempt to assume a role.
  • The shared credentials ini file at ~/.aws/credentials (more information)
    • Unless ENV['AWS_SDK_CONFIG_OPT_OUT'] is set, the shared configuration ini file at ~/.aws/config will also be parsed for credentials.
  • From an instance profile when running on EC2, or from the ECS credential provider when running in an ECS container with that feature enabled.
  • If using ~/.aws/config or ~/.aws/credentials a :profile option can be used to choose the proper credentials.

Shared configuration is loaded only a single time, and credentials are provided statically at client creation time. Shared credentials do not refresh.

The SDK searches the following locations for a region:

  • ENV['AWS_REGION']
  • Unless ENV['AWS_SDK_CONFIG_OPT_OUT'] is set, the shared configuration files (~/.aws/credentials and ~/.aws/config) will also be checked for a region selection.

The region is used to construct an SSL endpoint. If you need to connect to a non-standard endpoint, you may specify the :endpoint option.

Configuration Options

You can also configure default credentials and region via Aws.config. In version 2, Aws.config is a vanilla Ruby hash, not a method like it was in version 1. The Aws.config hash takes precedence over environment variables.

require 'aws-sdk'

Aws.config.update({
  region: 'us-west-2',
  credentials: Aws::Credentials.new('akid', 'secret')
})

Valid region and credentials options are:

You may also pass configuration options directly to resource and client constructors. These options take precedence over the environment and Aws.config defaults.

# resource constructors
ec2 = Aws::EC2::Resource.new(region:'us-west-2', credentials: credentials)

# client constructors
ec2 = Aws::EC2::Client.new(region:'us-west-2', credentials: credentials)

Please take care to never commit credentials to source control. We strongly recommended loading credentials from an external source.

require 'aws-sdk'
require 'json'
creds = JSON.load(File.read('secrets.json'))
Aws.config[:credentials] = Aws::Credentials.new(creds['AccessKeyId'], creds['SecretAccessKey'])

API Clients

Construct a service client to make API calls. Each client provides a 1-to-1
mapping of methods to API operations. Refer to the
API documentation
for a complete list of available methods.

# list buckets in Amazon S3
s3 = Aws::S3::Client.new
resp = s3.list_buckets
resp.buckets.map(&:name)
#=> ["bucket-1", "bucket-2", ...]

API methods accept a hash of additional request parameters and return
structured response data.

# list the first two objects in a bucket
resp = s3.list_objects(bucket: 'aws-sdk-core', max_keys: 2)
resp.contents.each do, object, puts "#{object.key} => #{object.etag}"
end

Paging Responses

Many AWS operations limit the number of results returned with each response.
To make it easy to get the next page of results, every AWS response object
is enumerable:

# yields one response object per API call made, this will enumerate
# EVERY object in the named bucket
s3.list_objects(bucket:'aws-sdk').each do, response, puts response.contents.map(&:key)
end

If you prefer to control paging yourself, response objects have helper methods
that control paging:

# make a request that returns a truncated response
resp = s3.list_objects(bucket:'aws-sdk')

resp.last_page? #=> false
resp.next_page? #=> true
resp = resp.next_page # send a request for the next response page
resp = resp.next_page until resp.last_page?

Waiters

Waiters are utility methods that poll for a particular state. To invoke a
waiter, call #wait_until on a client:

begin
  ec2.wait_until(:instance_running, instance_ids:['i-12345678'])
  puts "instance running"
rescue Aws::Waiters::Errors::WaiterFailed => error
  puts "failed waiting for instance running: #{error.message}"
end

Waiters have sensible default polling intervals and maximum attempts. You can
configure these per call to #wait_until. You can also register callbacks
that are triggered before each polling attempt and before waiting.
See the API documentation for more examples and for a list of supported
waiters per service.

Resource Interfaces

Resource interfaces are object oriented classes that represent actual
resources in AWS. Resource interfaces built on top of API clients and provide
additional functionality. Each service gem contains its own resource interface.

s3 = Aws::S3::Resource.new

# reference an existing bucket by name
bucket = s3.bucket('aws-sdk')

# enumerate every object in a bucket
bucket.objects.each do, obj, puts "#{obj.key} => #{obj.etag}"
end

# batch operations, delete objects in batches of 1k
bucket.objects(prefix: '/tmp-files/').delete

# single object operations
obj = bucket.object('hello')
obj.put(body:'Hello World!')
obj.etag
obj.delete

REPL - AWS Interactive Console

The aws-sdk gem ships with a REPL that provides a simple way to test
the Ruby SDK. You can access the REPL by running aws-v3.rb from the command line.

$ aws-v3.rb
Aws> ec2.describe_instances.reservations.first.instances.first
[Aws::EC2::Client 200 0.216615 0 retries] describe_instances()
<struct
 instance_id="i-1234567",
 image_id="ami-7654321",
 state=<struct  code=16, name="running">,
 ...>

You can enable HTTP wire logging by setting the verbose flag:

$ aws-v3.rb -v

In the REPL, every service class has a helper that returns a new client object.
Simply downcase the service module name for the helper:

  • Aws::S3 => s3
  • Aws::EC2 => ec2
  • etc

Versioning

This project uses semantic versioning. You can safely
express a dependency on a major version and expect all minor and patch versions
to be backwards compatible.

This library is distributed under the
Apache License, version 2.0

copyright 2013. amazon web services, inc. all rights reserved.

licensed under the apache license, version 2.0 (the "license");
you may not use this file except in compliance with the license.
you may obtain a copy of the license at

    http://www.apache.org/licenses/license-2.0

unless required by applicable law or agreed to in writing, software
distributed under the license is distributed on an "as is" basis,
without warranties or conditions of any kind, either express or implied.
see the license for the specific language governing permissions and
limitations under the license.

主要指标

概览
名称与所有者aws/aws-sdk-ruby
主编程语言Ruby
编程语言Ruby (语言数: 6)
平台
许可证Apache License 2.0
所有者活动
创建于2011-07-14 22:21:47
推送于2025-04-24 21:08:41
最后一次提交
发布数1209
最新版本名称v2.11.632 (发布于 2020-11-20 19:46:14)
第一版名称1.0.0 (发布于 )
用户参与
星数3.6k
关注者数138
派生数1.2k
提交数7.8k
已启用问题?
问题数1657
打开的问题数22
拉请求数1218
打开的拉请求数3
关闭的拉请求数284
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?