Ruby S3

Ruby AWS::S3

Creating a Connection

This creates a connection so that you can interact with the server.

 :server => '',
 :use_ssl => true,
 :access_key_id => 'my-access-key',
 :secret_access_key => 'my-secret-key'

Listing Owned Buckets

This gets a list of AWS::S3::Bucket objects that you own. This also prints out the bucket name and creation date of each bucket.

AWS::S3::Service.buckets.each do |bucket|
 puts "#{}\t#{bucket.creation_date}"

The output will look something like this:

mahbuckat1 2011-04-21T18:05:39.000Z mahbuckat2 2011-04-21T18:05:48.000Z mahbuckat3 2011-04-21T18:07:18.000Z

Creating a Bucket

This creates a new bucket called my-new-bucket


Listing a Bucket’s Content

This gets a list of hashes with the contents of each object This also prints out each object’s name, the file size, and last modified date.

new_bucket = AWS::S3::Bucket.find('my-new-bucket')
new_bucket.each do |object|
 puts "#{object.key}\t#{object.about['content-length']}\t#{object.about['last-modified']}"

The output will look something like this if the bucket has some files:

myphoto1.jpg 251262 2011-08-08T21:35:48.000Z

myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

Deleting a Bucket


The Bucket must be empty! Otherwise it won’t work!


Forced Delete for Non-empty Buckets

AWS::S3::Bucket.delete('my-new-bucket', :force => true)

Creating an Object

This creates a file hello.txt with the string "Hello World!"
 'Hello World!',
 :content_type => 'text/plain'

Change an Object’s ACL

This makes the object hello.txt to be publicly readable, and secret_plans.txt to be private.

policy = AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket')
policy.grants = [ AWS::S3::ACL::Grant.grant(:public_read) ]
AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket', policy)
policy = AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket')
policy.grants = []
AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket', policy)

Download an Object (to a file)

This downloads the object poetry.pdf and saves it in /home/larry/documents/

open('/home/larry/documents/poetry.pdf', 'w') do |file|'poetry.pdf', 'my-new-bucket') do |chunk|

Delete an Object

This deletes the object goodbye.txt

AWS::S3::S3Object.delete('goodbye.txt', 'my-new-bucket')

Generate Object Download URLs (signed and unsigned)

This generates an unsigned download URL for hello.txt. This works because we made hello.txt public by setting the ACL above. This then generates a signed download URL for secret_plans.txt that will work for 1 hour. Signed download URLs will work for the time period even if the object is private (when the time period is up, the URL will stop working).

puts AWS::S3::S3Object.url_for(
 :authenticated => false
puts AWS::S3::S3Object.url_for(
 :expires_in => 60 * 60

The output of this will look something like: