Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Ruby on Rails (http://www.go4expert.com/articles/ruby/)
-   -   Accessing Amazon S3 In Ruby (http://www.go4expert.com/articles/accessing-amazon-s3-ruby-t29284/)

asha 10Nov2012 13:46

Accessing Amazon S3 In Ruby
 
Amazon's Simple Storage Service is a very cheap and reliable storage infrastructure with a pay-as-you-go payment plan where you only need to pay for what you use. Amazon S3 provides 99.999999% data reliability, high data availability at a very nominal cost. It is one of the most suited solutions for small websites/businesses who can spend according to their usage as well get the best storage solution.

S3 Basics



Amazon S3 has a concept of buckets which is analogous to directories, but sub-directories i.e. buckets within buckets cannot be created. In the following code example we'll look a basic bucket operations.

Code: Ruby

#!/usr/local/bin/ruby

require 'rubygems'
require 'aws-sdk'

# set credentials
AWS.config({
    :access_key_id => '<AccessKey>',
    :secret_access_key => '<SecretKey>',
})

# create new S3 object
s3 = AWS::S3.new

# create a new bucket
s3.buckets.create('my_shiny_new_bucket')

# print bucket names
s3.buckets.each do |b|
    puts b.name
end

# select & list objects inside a bucket
my_bucket = s3.buckets['my_shiny_new_bucket']

# iterate through list of object and print the name
my_bucket.objects.each do |obj|
  puts obj.key
end


Working With S3 Objects



In S3 terminology files inside buckets are called objects, here we'll be looking a uploading, downloading & deleting objects. Please follow the code example to understand the usage and various ways to accomplish a task.

Code: Ruby

#!/usr/local/bin/ruby

require 'rubygems'
require 'aws-sdk'

# set credentials
AWS.config({
    :access_key_id => '<AccessKey>',
    :secret_access_key => '<SecretKey>',
})

# create new S3 object
s3 = AWS::S3.new

# select a bucket
my_bucket = s3.buckets['my_shiny_new_bucket']

# listing objects in a bucket
my_bucket.objects.each do |obj|
  puts obj.key
end

# create a new file with some text
my_bucket.objects.create('my_filename.txt','My personal text')

# create a new file
my_image_obj =  bucket.objects.create('my_image.jpg', '')

# upload an existing file
# by specifying the path of the file
my_image_obj.write(Pathname.new('/home/pradeep/image.jpg'))

# same as the above
my_image_obj.write(:file => '/home/pradeep/image.jpg')

# You can also pass a file object
file = File.open('/home/pradeep/image.jpg', 'r')
my_image_obj.write(file)

# Downloading files
# select file
my_file = my_bucket.objects['my_filename.txt']

# print the file contents
puts my_file.read

# writing the contents to a file
File.open('my_local_file.txt', 'w') do |file|
    file.write(my_file.read)
end

# in case of a large file, you may write in chunks
File.open('my_local_image.jpg', 'w') do |file|
  bucket.objects['my_image.jpg'].read do |chunk|
    file.write(chunk)
  end
end

# Deleting objects
my_bucket.objects.delete('my_very_old_file.txt')


Advanced Usage & Access Control



In this section let's look into some advanced usage, and how to set access permissions on objects. First the following code snippet will help you know and understand some advanced features/functions of Amazon S3.

Code: Ruby

#!/usr/local/bin/ruby

require 'rubygems'
require 'aws-sdk'

# set credentials
AWS.config({
    :access_key_id => '<AccessKey>',
    :secret_access_key => '<SecretKey>',
})

# create new S3 object
s3 = AWS::S3.new

# select a bucket
my_bucket = s3.buckets['my_shiny_new_bucket']

# if you have to delete multiple files, you can delete them with a single request/call
my_bucket.objects.delete('my_file1.txt','my_file2.txt','my_file3.txt')

# you can list file based on any prefix string you specify
# the code below will list all objects key names starting with 'my_'
my_bucket.objects.with_prefix('my_').each do |obj|
  puts obj.key
end

# you can check if a bucket exists
# the next line does not make any call to the S3 server
my_bucket = s3.buckets['my_old_bucket']

# the next line returns true or false
my_bucket.exists?


Next, let us look at ACL using which we can control who gets access like read,write, etc.

Code: Ruby

#!/usr/local/bin/ruby

require 'rubygems'
require 'aws-sdk'

# set credentials
AWS.config({
    :access_key_id => '<AccessKey>',
    :secret_access_key => '<SecretKey>',
})

# create new S3 object
s3 = AWS::S3.new

# select a bucket
my_bucket = s3.buckets['my_shiny_new_bucket']

# set the ACL of an object while uploading
obj.write(File.open('path/to/file.txt', 'r'), :acl => :public_read)

# you may specify any of the following ACL name
# :private
# :public_read
# :public_read_write
# :authenticated_read


You can also lookup AWS Ruby API docs at http://aws.amazon.com/sdkforruby/ for more advanced ACLs configurations.


All times are GMT +5.5. The time now is 13:23.