Share This Article
The AWS Ruby SDK provides a Ruby API for AWS Infrastructure Services.
You can install the aws-sdk gem with the following command:
gem install aws-sdk
Or add it to your Gemfile:
gem 'aws-sdk'
Configuration AWS Ruby SDK
Before interacting with AWS Services, you need to configure the aws-sdk gem with your credentials.
To do this, you can use the Aws.config
class method.
require 'aws-sdk'
Aws.config.update({
region: 'us-west-2',
credentials: Aws::Credentials.new('AKID', 'SECRET')
})
Using AWS Ruby SDK
The AWS SDK for Ruby gives you access to all the AWS infrastructure services.
You need to require the corresponding service module to use a specific service.
For example, to work with S3, you can do the following:
require 'aws-sdk'
s3 = Aws::S3::Client.new
resp = s3.list_buckets
resp.buckets.each do |bucket|
puts bucket.name
end
Alternatively, you can use the Aws
module to get a service client instance.
require 'aws-sdk'
s3 = Aws.s3
resp = s3.list_buckets
resp.buckets.each do |bucket|
puts bucket.name
end
You can also get service resources, high-level interfaces that provide a richer object-oriented experience.
require 'aws-sdk'
s3 = Aws::S3::Resource.new
s3.buckets.each do |bucket|
puts bucket.name
end
Creating an S3 Bucket
To create an S3 bucket, you can use the #create_bucket
method.
bucket = s3.create_bucket({
bucket: "MY_BUCKET_NAME",
})
Deleting an S3 Bucket
To delete an S3 bucket, you can use the #delete_bucket
method.
s3.delete_bucket({
bucket: "MY_BUCKET_NAME",
})
Creating an S3 Object
If you want to create an S3 object, you can use the #put_object
method.
s3.put_object({
body: "Hello, World!",
bucket: "MY_BUCKET_NAME",
key: "hello.txt",
})
Documentation
You can find the AWS SDK for Ruby API Reference and User Guide documentation right here.
Summary
This article taught you how to install and configure the AWS Ruby SDK. You’ve also learned how to create a client for the AWS service you want to interact with.