Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Creating a bucket in Amazon S3 through an irb session

1) Log into an irb session, and enter your S3 login details.
   1  
   2  require 'rubygems'
   3  require 'aws/s3'
   4  
   5    AWS::S3::Base.establish_connection!(
   6      :access_key_id     => 'REPLACE_ME',
   7      :secret_access_key => 'REPLACE_ME'
   8    )

output:
=> #<AWS::S3::Connection:0xb75e0594 @http=#<Net::HTTP s3.amazonaws.com:80 open=false>, @secret_access_key="", @options={:server=>"s3.amazonaws.com", :access_key_id=>"", :port=>80, :secret_access_key=>"", :persistent=>true}, @access_key_id="19S45GYAGWK8DC2B8VG2">

2) Browse the existing buckets.
   1  AWS::S3::Service.buckets

output:
=> [#<AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=>"ogg.twitteraudio.com", "creation_date"=>Sat Apr 26 10:40:16 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=>"t1000", "creation_date"=>Fri Apr 25 21:35:21 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=>"t2000", "creation_date"=>Fri Apr 25 21:53:15 UTC 2008}>]

3) Browse the buckets in a programmatical way.
   1  AWS::S3::Service.buckets.each {|b| puts b.name}

output:
ogg.twitteraudio.com
t1000
t2000


4) Add a new bucket called t3000.
   1  AWS::S3::Bucket.create('t3000')

output:
=> true

5) Observe adding the bucket again doesn't cause an error.
   1  AWS::S3::Bucket.create('t3000')

output:
=> true

6) View the buckets again.
   1  AWS::S3::Service.buckets

output:
=> [#<AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=>"ogg.twitteraudio.com", "creation_date"=>Sat Apr 26 10:40:16 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=>"t1000", "creation_date"=>Fri Apr 25 21:35:21 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=>"t2000", "creation_date"=>Fri Apr 25 21:53:15 UTC 2008}>]

Note: You would expect t3000 to be in there however it didn't appear possibly because of the bucket permissions.

7) Let's then look for bucket t3000.
   1  t3000 = AWS::S3::Bucket.find('t3000')

output:
=> #<AWS::S3::Bucket:0xb76df724 @object_cache=[], @attributes={"prefix"=>nil, "name"=>"t3000", "marker"=>nil, "max_keys"=>1000, "is_truncated"=>false, "xmlns"=>"http://s3.amazonaws.com/doc/2006-03-01/"}>

8) Now that we've found the bucket let's upload a text file called works.txt.
   1  file = "works.txt"

output:
=> "works.txt"
   1  AWS::S3::S3Object.store(file, open(file), 't3000', :access => :public_read)

output:
=> #<AWS::S3::S3Object::Response:0x-608926458 200 OK>

9) Setting the file access to :public_read allows us to view the file from the http location http://t3000.s3.amazonaws.com/works.txt

References:
http://amazon.rubyforge.org/
upload_to_s3 - Ruby S3 upload client [dzone.com]

*update: 14:30 30 April 2008 *
I didn't use Bucket.objects(:reload) which is the reason why the bucket t3000 didn't show up with the statement Service.buckets

Reference: spatten design - Amazon S3, Ruby and Rails slides [spattendesign.com]

Simple S3 utils - copy bucket to bucket

Some simple S3 utils in Ruby

Specifically written to copy one bucket to another (for testing on production on staging)

PLEASE BE CAREFUL WITH THIS - THERE IS CODE THAT DELETES ALL CONTENTS OF A BUCKET

Example:
   1  
   2  a = AmazoneS3Asset.new
   3  a.copy_over_bucket("myapp_production", "myapp_production")


   1  
   2  require 'aws/s3'
   3  require 'mechanize'
   4  
   5  class AmazonS3Asset
   6    
   7    include AWS::S3
   8    S3ID = "your s3 id"
   9    S3KEY = "your s3 key"
  10    
  11    def initialize
  12      puts "connecting..."
  13      AWS::S3::Base.establish_connection!(
  14        :access_key_id     => S3ID,
  15        :secret_access_key => S3KEY
  16      )
  17    end
  18  
  19    def delete_key(bucket, key)
  20      if exists?(bucket, key) 
  21        S3Object.delete key, bucket
  22      end
  23    end
  24    
  25    def empty_bucket(bucket)
  26      bucket_keys(bucket).each do |k|
  27        puts "deleting #{k}"
  28        delete_key(bucket,k)
  29      end
  30    end
  31    
  32    def bucket_keys(bucket)
  33      b = Bucket.find(bucket)
  34      b.objects.collect {|o| o.key}
  35    end
  36  
  37    def copy_over_bucket(from_bucket, to_bucket)
  38      puts "Replacing #{to_bucket} with contents of #{from_bucket}"
  39      #delete to_bucket
  40      empty_bucket(to_bucket)
  41      bucket_keys(from_bucket).each do |k|
  42        copy_between_buckets(from_bucket, to_bucket, k)
  43      end
  44    end
  45    
  46    def copy_between_buckets(from_bucket, to_bucket, from_key, to_key = nil)
  47      if exists?(from_bucket, from_key)
  48        to_key = from_key if to_key.nil?
  49        puts "Copying #{from_bucket}.#{from_key} to #{to_bucket}.#{to_key}"
  50        url = "http://s3.amazonaws.com/#{from_bucket}/#{from_key}"
  51        filename = download(url)
  52        store_file(to_bucket,to_key,filename)
  53        File.delete(filename)
  54        return "http://s3.amazonaws.com/#{to_bucket}/#{to_key}"
  55      else
  56        puts "#{from_bucket}.#{from_key} didn't exist"
  57        return nil
  58      end
  59    end
  60  
  61    def store_file(bucket, key, filename)
  62       puts "Storing #{filename} in #{bucket}.#{key}"
  63       S3Object.store(
  64        key,
  65        File.open(filename),
  66        bucket,
  67        :access => :public_read
  68        )
  69    end
  70  
  71    def download(url, save_as = nil)
  72      if save_as.nil?
  73        Dir.mkdir("amazon_s3_temp") if !File.exists?("amazon_s3_temp")
  74        save_as = File.join("amazon_s3_temp",File.basename(url))
  75      end
  76      begin
  77        puts "Saving #{url} to #{save_as}"
  78        agent = WWW::Mechanize.new {|a| a.log = Logger.new(STDERR) }
  79        img = agent.get(url)
  80        img.save_as(save_as)
  81        return save_as
  82      rescue
  83        raise "Failed on " + url + "  " + save_as
  84      end
  85    end
  86  
  87    def exists?(bucket,key)
  88      begin
  89        res = S3Object.find key, bucket
  90      rescue 
  91        res = nil
  92      end
  93      return !res.nil?
  94    end
  95        
  96  end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS