Learning AWS SDK for Ruby and basic S3 operations with sample code
This section introduces you about how to install AWS SDK for Ruby and how to create S3 buckets, put objects, get objects using the sample code, and explains how the sample code runs as well.
Getting ready
The AWS SDK for Ruby provides a Ruby API operation and enables developer help complicated coding by providing Ruby classes. New users should start with AWS SDK for Ruby V2, as officially recommended.
To get started with AWS SDK for Ruby, it is necessary to install the following on your development machine:
- Ruby (https://www.ruby-lang.org/)
- Bundler (http://bundler.io/)
How to do it…
Proceed with the following steps to install the packages and run the sample application. We install the stable AWS SDK for Ruby v2 and download the sample code.
- Download the sample SDK application:
$ git clone https://github.com/awslabs/aws-ruby-sample.git $ cd aws-ruby-sample/
- Run the sample application:
$ bundle install $ ruby s3_sample.rb
How it works…
The sample code works as shown in the following diagram; initiating the credentials to allow access to Amazon S3, creating a bucket in a region, putting and getting objects into the bucket, and then finally deleting the objects and the bucket.
Now, let's run the sample application and see the output of the preceding command, which is shown in the following screenshot, and then follow the source code step by step:
Now, let's examine the sample code; the path is aws-ruby-sample/s3-sample.rb
.
The AWS::S3.new
method creates an AWS client:
s3 = AWS::S3.new
The s3.buckets.create
method creates an S3 bucket with the specified name defined as the bucket_name
variable in the standard US region by default:
uuid = UUID.new bucket_name = "ruby-sdk-sample-#{uuid.generate}" bucket = s3.bucket(bucket_name) bucket.create
The objects.put
method puts an object defined as the objects
variable in the bucket:
object = bucket.object('ruby_sample_key.txt') object.put(body: "Hello World!")
The object.public_url
method creates a public URL for the object:
puts object.public_url
The object.url_for(:read)
method creates a public URL to read an object:
puts object.url_for(:read)
The bucket.delete!
method deletes all the objects in a bucket, and then deletes the bucket:
bucket.delete!
The whole sample code is as follows:
#!/usr/bin/env ruby require 'rubygems' require 'bundler/setup' require 'aws-sdk' require 'uuid' s3 = Aws::S3::Resource.new(region: 'us-west-2') uuid = UUID.new bucket_name = "ruby-sdk-sample-#{uuid.generate}" bucket = s3.bucket(bucket_name) bucket.create object = bucket.object('ruby_sample_key.txt') object.put(body: "Hello World!") puts "Created an object in S3 at:" puts object.public_url puts "\nUse this URL to download the file:" puts object.presigned_url(:get) puts "(press any key to delete both the bucket and the object)" $stdin.getc puts "Deleting bucket #{bucket_name}" bucket.delete!
See also
- AWS SDK for the Ruby sample application, available at https://github.com/aws/aws-sdk-ruby
- Developer Guide available at http://docs.aws.amazon.com/AWSSdkDocsRuby/latest/DeveloperGuide/
- The API documentation available at http://docs.aws.amazon.com/sdkforruby/api/frames.html