Session | Basic Ruby S3 SDK Exercises
Basic Ruby S3 SDK Exercises
In this section, you'll use the Ruby AWS SDK to interact with the S3 service.
By default, AWS will use the configuration that is located in ~/.aws/configuration. This file contains the Access Key ID and Secret Access Key that you set when you typed $ aws configure.
Setup
- Make a new directory
s3_ruby_exercises. - Create a new file called
s3repl.rb. - Install the Ruby SDK on the command line with
$ gem install aws-sdk - Make a Gemfile:
touch Gemfileand add the following: - At the top of your
s3repl.rb, add the following code which will require the 'aws-sdk' and create an object you will use to interact with S3:
source "https://rubygems.org" gem 'aws-sdk', '~> 3'
require 'aws-sdk-s3' s3 = Aws::S3::Client.new(region: 'us-east-1')
Remember, you don't need to set env variables or import keys since the SDK looks in your ~/.aws/configuration file.
Creating Buckets
Use the Client#create_bucket documentation (and/or Google) to write code that creates an empty bucket in your account.
Extensions:
- Can you accept a command line argument for the bucket name? Need help? Try this code in a file:
puts "Hello, #{ARGV[0]}"And run it usingruby myfile.rb bob - Instead of a command line argument, can you accept a dynamic value for the bucket name that is entered while the program is running? Hint:
puts "How are you feeling today?" response = gets.chomp
$ aws configure. Using code, can you change the region where your bucket is created?Uploading Objects
Use the Client#put_object documentation (and/or Google) to write code that uploads a file (object) in the bucket you just created.
Extensions:
- Can you accept a dynamic value for the object to be uploaded that is entered while the program is running?
Listing Objects
Use the Client#list_objects documentation (and/or Google) to write code that lists all of the objects in a specific bucket.
Extensions:
- Can you accept a dynamic value for the bucket name that is entered while the program is running? Hint:
- Can you accept a dynamic value for how many objects within a bucket should be returned that is entered while the program is running?
Deleting Objects
Use the Client#delete_bucket documentation (and/or Google) to write code that deletes an object in the bucket.
Extensions:
- Can you accept a dynamic value for the object that is entered while the program is running?
Listing Buckets
Use the Client#list_buckets documentation (and/or Google) to write code that lists all of the buckets in your account.
Full REPL Functionality
Use the code that you wrote in the above exercises to create a full REPL that has a similar interaction pattern to this:
What do you want to do? Enter one of the following: b: list buckets o: list objects in specific bucket mb: make bucket u: upload file d: delete file q: quit ------------ b ------------ Here are your buckets: - myfirstwebsite - images What do you want to do? Enter one of the following: b: list buckets o: list objects in specific bucket mb: make bucket u: upload file d: delete file q: quit ------------ mb ------------ What would you like to call your bucket? ------------ documents ------------ Here are your buckets: - myfirstwebsite - images - documents What do you want to do? Enter one of the following: b: list buckets o: list objects in specific bucket mb: make bucket u: upload file d: delete file q: quit ------------ o ------------ For which bucket would you like to list your objects? ------------ images ------------ The objects in your images bucket are: - penguin.jpg - logo.png - banner.jpg What do you want to do? Enter one of the following: b: list buckets o: list objects in specific bucket mb: make bucket u: upload file d: delete file q: quit ------------ q ------------ Goodbye!
Email Notifications Upon Object Upload
This section goes beyond what we did in Class 1 to incorporate AWS Simple Notification Service (SNS). The instructions below will set you up with a program where you receive an email whenever an object is uploaded to a specific bucket.
If you'd like to read more about SNS before diving in, click here.
First, we'll set up an SNS topic, then set up a subscription to that topic for a specific email address.
- In your AWS Management Console in the browser, select "SNS" from the service list.