Accessing Amazon S3 In Ruby

Discussion in 'Ruby on Rails' started by asha, Nov 10, 2012.

  1. asha

    asha New Member

    Joined:
    Nov 9, 2006
    Messages:
    44
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Homemaker
    Location:
    Kolkata
    Home Page:
    http://whatanindianrecipe.com
    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:
    #!/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:
    #!/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:
    #!/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:
    #!/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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice