Creating a bucket in Amazon S3 through an irb session
require 'rubygems' require 'aws/s3' AWS::S3::Base.establish_connection!( :access_key_id => 'REPLACE_ME', :secret_access_key => 'REPLACE_ME' )
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.
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.
AWS::S3::Service.buckets.each {|b| puts b.name}
output:
ogg.twitteraudio.com
t1000
t2000
4) Add a new bucket called t3000.
AWS::S3::Bucket.create('t3000')
output:
=> true
5) Observe adding the bucket again doesn't cause an error.
AWS::S3::Bucket.create('t3000')
output:
=> true
6) View the buckets again.
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.
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.
file = "works.txt"
output:
=> "works.txt"
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]