The AWS SDK for Ruby is available from RubyGems. With V3 modularization, you should pick the specific AWS service gems to install.
gem 'aws-sdk-s3', '~> 1'
gem 'aws-sdk-ec2', '~> 1'
Alternatively, the aws-sdk gem contains every available AWS service gem. This
gem is very large; it is recommended to use it only as a quick way to migrate
from V2 or if you depend on many AWS services.
gem 'aws-sdk', '~> 3'
Please use a pessimistic version constraint on the major version when depending on service gems.
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']~/.aws/credentials. The location used can be changed with the AWS_CREDENTIALS_FILE ENV variable.aws_access_key_id, aws_secret_access_key, aws_session_token)web_identity_token_file, role_arn, source_profile)role_arn, source_profile)credential_process)sso_session, sso_account_id, sso_role_name, sso_region)ENV['AWS_SDK_CONFIG_OPT_OUT'] is set, the shared configuration ini file at ~/.aws/config will also be parsed for 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']ENV['AMAZON_REGION']ENV['AWS_DEFAULT_REGION']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.
You can also configure default credentials and the region via the Aws.config
hash. The Aws.config hash takes precedence over environment variables.
require 'aws-sdk-core'
Aws.config.update(
region: 'us-west-2',
credentials: Aws::Credentials.new('akid', 'secret')
)
Valid region and credentials options are:
:region - A string like us-west-2. See this page for a list of supported regions by service.:credentials - An instance of one of the following classes:Aws::CredentialsAws::AssumeRoleWebIdentityCredentialsAws::AssumeRoleCredentialsAws::SharedCredentialsAws::SSOCredentialsAws::LoginCredentialsAws::ProcessCredentialsAws::InstanceProfileCredentialsAws::ECSCredentialsAws::CognitoIdentityCredentialsYou may also pass configuration options directly to Client and Resource
constructors. These options take precedence over the environment and
Aws.config defaults. A :profile Client option can also be used to choose a
specific profile defined in your configuration file.
# using a credentials object
ec2 = Aws::EC2::Client.new(region: 'us-west-2', credentials: credentials)
# using a profile name
ec2 = Aws::EC2::Client.new(profile: 'my_profile')
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']
)
For more information on how to configure credentials, see the developer guide for configuring AWS SDK for Ruby.
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', max_keys: 2)
resp.contents.each do |object|
puts "#{object.key} => #{object.etag}"
end
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 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 are object oriented classes that represent actual resources in AWS. Resource interfaces built on top of API clients and provide additional functionality.
Only a few services implement a resource interface. They are defined by hand in JSON and have limitations. Please use the Client API instead.
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
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
[1] pry(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:
s3 => #<Aws::S3::Client>ec2 => #<Aws::EC2::Client>The AWS SDK for Ruby has optional functionality that requires the AWS Common Runtime (CRT) bindings to be included as a dependency with your application. This functionality includes: * CRC-32c support for S3 Additional Checksums
AWS CRT bindings are in developer preview and are available in the
the aws-crt gem. You can install them by adding the aws-crt gem to your Gemfile.
Please use any of these resources for getting help:
aws-sdk-ruby.F
| 日期 | 榜单 | 排名 | 周期新增 | 总 STAR |
|---|