Ruby to S3, an introduction guide to its AWS SDK

And finally we land into the world of Ruby, which has one of the most richest and finest SDKs when it comes to AWS.

This tutorial is based on Ubuntu WSL, the subshell for the Redmond’s OS, and you can download it easily from the Microsoft store.

Although recommended for the purposes of this guide, I think you can make it even if you are on a vanilla Ubuntu installation or virtual machine.

The Ruby programming language

The first thing is to install Ruby on WSL, I chose RVM to install and manage different versions of the language on the same operating system. Once you’re logged on WSL, issue the following commands, and everything should work fine:

$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ curl -sSL https://get.rvm.io | bash -s stable
$ source ~/.rvm/scripts/rvm
$ rvm install 2.6.3
$ ruby -v

You should see the correct Ruby version at your terminal once you are done. The second thing is to install Python, pip and then the AWS CLI and configure it:

$ sudo apt-get install python
$ python --version
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
$ pip install awscli --upgrade
$ aws configure

And now we create and setup our project with a Gemfile, in the following way:

$ mkdir aws-s3-ruby-tutorial
$ cd aws-s3-ruby-tutorial
$ vim Gemfile
source 'https://rubygems.org'
gem 'aws-sdk'
gem 'highline'

We’re ready to download our dependencies:

$ gem install bundler
$ bundle install

Ok then, let’s start coding our create_buckets.rb:

require 'aws-sdk-s3'
require 'securerandom'
require 'highline'


profile_name = 'default'
region = 'us-west-2'

cli = HighLine.new
asked_name = cli.ask "Please enter a name for your Bucket:"


bucket_name = asked_name + '-' + SecureRandom.uuid

s3 = Aws::S3::Client.new(profile: profile_name, region: region)

begin
 s3.create_bucket(bucket: bucket_name)
 puts 'You created a new Bucket: ' + bucket_name
rescue Exception => e
 puts 'Something went wrong, please retry:'
 puts e.message
end

And here we have show_buckets.rb:

require 'aws-sdk-s3'
require 'pp'


s3 = Aws::S3::Client.new(profile: 'default', region: 'us-west-2')

buckets = s3.list_buckets.buckets

puts 'You have no Buckets yet.' if buckets.empty?

buckets.each do |b|
 puts b.name
 objects = s3.list_objects({ bucket: b.name }).contents
 pp objects unless objects.empty?
end

And again we have put_files.rb:

require 'aws-sdk-s3'
require 'highline'


cli = HighLine.new
bucket_name = cli.ask "Please enter the name of your Bucket:"
file_name = cli.ask "Please enter the name of your file:"

profile_name = 'default'
region = 'us-west-2'

s3 = Aws::S3::Client.new(profile: profile_name, region: region)

file_handler = File.open(file_name, "r")

s3.put_object({
 body: file_handler, 
 bucket: bucket_name, 
 key: file_name, 
})

file_handler.close

Fine, let’s see our delete_objects.rb:

require 'aws-sdk-s3'
require 'highline'


cli = HighLine.new
bucket_name = cli.ask "Please enter the name of your Bucket:"
file_name = cli.ask "Please enter the name of your file:"

profile_name = 'default'
region = 'us-west-2'

s3 = Aws::S3::Client.new(profile: profile_name, region: region)

s3.delete_object({
 bucket: bucket_name,
 key: file_name
})

And last but not least, delete_buckets.rb:

require 'aws-sdk-s3'
require 'highline'


cli = HighLine.new
bucket_name = cli.ask "Please enter the name of the Bucket to delete:"

profile_name = 'default'
region = 'us-west-2'

s3 = Aws::S3::Client.new(profile: profile_name, region: region)

objects = s3.list_objects({ bucket: bucket_name }).contents

objects.each do |object|
 s3.delete_object({
  bucket: bucket_name, 
  key: object.key
 })
end

s3.delete_bucket({ bucket: bucket_name })

As you can see Ruby’s code is cleaner and easier to read than other languages, in that it is based on the English way of talking and its grammar.

Don’t forget to look at the GitHub repository for this tutorial if you want to clone the full code, and follow me, if you wish.

Take also the time to read the documentation to unleash the power of this piece of software. You can also compare other SDKs for AWS.

And remember you can always contact me in person, should you have any suggestions or things you don’t understand; feel free to do it. Till then, stay tuned and I’ll see you next time with another tutorial.

Did you like this post? Please comment here below and share it on your preferred social networks, thank you!

2 thoughts on “Ruby to S3, an introduction guide to its AWS SDK

  1. Hey Mirko,
    great tutorial..
    I can’t tell you how interesting it is!

Leave a Reply

Give me your opinion, I will be grateful.