RAWS

RAWS is a Ruby library for Amazon Web Service (AWS).

Usage

SQS (Amazon Simple Queue Service)

require 'rubygems'
require 'raws'

RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_

RAWS::SQS.create_queue('test_queue')
sleep 60

RAWS::SQS['test_queue'].send('hello1')
RAWS::SQS['test_queue'].send('hello2')
RAWS::SQS['test_queue'].send('hello3')

RAWS::SQS['test_queue'].receive.each do |msg|
  p msg.body
  msg.delete
end

RAWS::SQS.delete_queue('test_queue')

SDB (Amazon SimpleDB)

require 'rubygems'
require 'raws'

RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_

RAWS::SDB.create_domain('test_domain')
sleep 60

RAWS::SDB['test_domain'].put('1', 'a' => '10')
RAWS::SDB['test_domain'].put('2', 'b' => '10')
RAWS::SDB['test_domain'].put('3', 'c' => '10')

p RAWS::SDB['test_domain'].get('1')
p RAWS::SDB['test_domain'].get('2')
p RAWS::SDB['test_domain'].get('3')

RAWS::SDB['test_domain'].all.each do |key, data|
  p [key, data]
end

RAWS::SDB['test_domain'].all.filter('a = ?', 10).each do |key, data|
  p [key, data]
end

RAWS::SDB['test_domain'].delete('1')
RAWS::SDB['test_domain'].delete('2')
RAWS::SDB['test_domain'].delete('3')

RAWS::SDB.delete_domain('test_domain')

require 'rubygems'
require 'raws'

RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_

class Foo
  include RAWS::SDB::Model
  self.domain_name = 'test_domain'
end

Foo.create_domain
sleep 60

foo1 = Foo.new('a' => '10', 'b' => '100', 'c' => ['1000a', '1000b'])
foo1.save

foo2 = Foo.new
foo2['a'] = '20'
foo2['b'] = ['200a', '200b']
foo2['c'] = '2000'
foo2.save

Foo.all.each do |a|
  p [a['a'], a['b'], a['c']]
end

Foo.delete_domain

S3 (Amazon Simple Storage Service)

require 'rubygems'
require 'raws'

RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_

RAWS::S3.create_bucket('test-bucket')

RAWS::S3['test-bucket'].put('helloworld') do |request|
  request.header['x-amz-acl'] = 'public-read'
  request.header['content-type'] = 'text/plain'
  request.header['content-length'] = "Hello World\n".size * 1000
  request.send do |io|
    1000.times do
      io.write "Hello World\n"
    end
  end
end

RAWS::S3['test-bucket'].get('helloworld') do |request|
  response = request.send
  p response.header
  response.receive do |io|
    while chunk = io.read
      print chunk
    end
  end
end

acl = RAWS::S3['test-bucket'].acl
p [acl.owner.id, acl.owner.name]
acl.grants.each do |grant|
  p [grant.permission, grant.class.name]
end

acl = RAWS::S3['test-bucket'].acl('helloworld')
p [acl.owner.id, acl.owner.name]
acl.grants.each do |grant|
  p [grant.permission, grant.class.name]
end

acl.grants.delete_if do |grant|
  grant.is_a? RAWS::S3::ACL::AllUsers
end
acl.save

acl.grants << RAWS::S3::ACL::AllUsers.new('READ')
acl.save

RAWS::S3.delete_bucket('test-bucket')

require 'rubygems'
require 'raws'

RAWS.aws_access_key_id = _AWS_ACCESS_KEY_ID_
RAWS.aws_secret_access_key = _AWS_SECRET_ACCESS_KEY_

class Foo
  include RAWS::S3::Model
  self.bucket_name = 'test-bucket'
end

Foo.create_bucket

foo = Foo.new('helloworld')
foo.['ja'] = 'こんにちは'
foo.send(
  'x-amz-acl' => 'public-read',
  'content-type' => 'text/plain',
  'content-length' => "Hello World\n".size * 1000
) do |io|
  1000.times do
    io.write "Hello World\n"
  end
end

foo = Foo.find('helloworld')
p foo.header
p foo.
foo.receive do |io|
  while chunk = io.read
    print chunk
  end
end

acl = Foo.acl
p [acl.owner.id, acl.owner.name]
acl.grants.each do |grant|
  p [grant.permission, grant.class.name]
end

acl = foo.acl
p [acl.owner.id, acl.owner.name]
acl.grants.each do |grant|
  p [grant.permission, grant.class.name]
end

acl.grants.delete_if do |grant|
  grant.is_a? RAWS::S3::ACL::AllUsers
end
acl.save

acl.grants << RAWS::S3::ACL::AllUsers.new('READ')
acl.save

Foo.delete_bucket

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but

    bump version in a commit by itself I can ignore when I pull)
    
  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009, 2010 Jun Kikuchi. See LICENSE for details.