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

About this user

James Robertson http://www.r0bertson.co.uk

« 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]

Using ruby to upload multiple files to Amazon S3

This ruby code builds upon the code S3 upload client for Ruby [dzone.com] by reading the account details and file details from XML files.

   1  
   2  #!/usr/bin/env ruby
   3  #file : rubys3file2upload.rb
   4  
   5  require 'rubygems'
   6  require 'aws/s3'
   7  require 'rexml/document'
   8  include REXML
   9  
  10  class S3FileUpload
  11  
  12    def initialize(xml_accounts_file, xml_upload_file)
  13      initialize_account(xml_accounts_file)
  14      main(xml_upload_file)
  15    end
  16  
  17    def initialize_account(accounts_file)
  18  
  19      file = File.new(accounts_file)
  20      doc = Document.new(file)
  21  
  22      account = doc.root.elements['records/access']
  23      h = Hash.new
  24      h[:access_key_id] = account.elements['key_id'].text.to_s
  25      h[:secret_access_key] = account.elements['secret'].text.to_s
  26      AWS::S3::Base.establish_connection!(h)
  27  
  28    end
  29  
  30    def main(xml_upload_file)
  31      file = File.new(xml_upload_file)
  32      doc = Document.new(file)
  33  puts doc
  34      doc.root.elements.each('records/file') do |f|
  35        local_file = f.elements['local'].text.to_s
  36        bucket = f.elements['bucket'].text.to_s
  37        mime_type = f.elements['mime_type'].text.to_s
  38        upload(local_file, mime_type, bucket)
  39      end
  40    end
  41  
  42    def upload(local_file, mime_type, bucket)
  43  
  44      base_name = File.basename(local_file)
  45  
  46      puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"
  47  
  48      AWS::S3::S3Object.store(
  49        base_name,
  50        File.open(local_file),
  51        bucket,
  52        :content_type => mime_type,
  53        :access => :public_read
  54      )
  55  
  56      puts "Uploaded!"
  57    end
  58  
  59  end
  60  
  61  if __FILE__ == $0
  62    s3fu = S3FileUpload.new('s3accounts.xml', 's3files2upload.xml')
  63  end


file: s3accounts.xml
   1  
   2  <s3accounts>
   3    <summary/>
   4    <records>
   5      <access id="100"><name>REPLACE_ME</name><key_id>REPLACE_ME</key_id><secret>REPLACE_ME</secret></access>
   6    </records>
   7  </s3accounts>


file: s3files2upload.xml
   1  
   2  <s3files2upload>
   3    <summary/>
   4    <records>
   5      <file>
   6        <local>autocomplete.html</local>
   7        <bucket>t2000</bucket>
   8        <mime_type>text/html</mime_type>
   9      </file>
  10      <file>
  11        <local>autocomplete2.html</local>
  12        <bucket>t2000</bucket>
  13        <mime_type>text/html</mime_type>
  14      </file>
  15      <file>
  16        <local>autocomplete3.html</local>
  17        <bucket>t2000</bucket>
  18        <mime_type>text/html</mime_type>
  19      </file>
  20    </records>
  21  </s3files2upload>


Note: The file given the correct permission could be read from http://t2000.s3.amazonaws.com/autocomplete.html

Reference: http://amazon.rubyforge.org/
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS