Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This is a simple script that puts a list of key/value pairs into a single SimpleDB item.
#!/usr/bin/ruby
#
# This is a simple script that puts a list of key/value pairs into a single SimpleDB item. It assumes that your
# Amazon credentials are stored in the environment variables "AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY".
#
# Examples:
#
# Insert a bunch of attributes for 'testitem' in the domain 'domain.name':
# ./put.rb -d domain.name -i testitem key1=val1 key2=val2-1 key2=val2-2 key3=val3
#
# Replace the key1 attribute with (key1, new_val1):
# ./put.rb -d domain.name -i testitem -r key1=new_val1
#
# Use a different separator to insert the pair (key4, val4=something):
# ./put.rb -d domain.name -i testitem -s ':' key4:val4=something
#
# Author: darren (darren@bizo.com)
# Date: 2009/10/12
#####
require 'rubygems'
require 'optparse'
require 'right_aws' # gem install right_aws
replace = false
domain = ""
item = ""
separator = "="
opts = OptionParser.new
opts.banner = "Usage: #{$0} [-r] [-s separator] -d DOMAIN -i ITEM key=value [key=value...]"
opts.on("-r", "--with-replacement", "replace pre-existing attributes") { |val|
replace = true
}
opts.on("-d", "--domain DOMAIN", "specify the domain to use") { |val|
domain = val
}
opts.on("-i", "--item-name ITEM", "specify the item to use") { |val|
item = val
}
opts.on("-s", "--separator SEPARATOR", "specify the key/value separator to use (default is '=')") { |val|
separator = val
}
opts.on("-h", "-?", "--help", "Display this screen" ) do
puts opts
exit
end
args = opts.parse(ARGV)
if (domain == "") then
puts "Error: No domain specified."
puts opts
exit 1
end
if (item == "") then
puts "Error: No item specified."
puts opts
exit 1
end
if args.size == 0 then
puts "Error: No key/value pairs specified"
puts opts
exit 1
end
attributes = Hash.new
args.each do |arg|
splitString = arg.split(separator, 2)
if (splitString.size != 2) then
puts "Error: the key/value pair [#{arg}] cannot be parsed because it does not contain the separator '#{separator}'"
puts opts
exit 1
end
if (attributes.has_key?(splitString[0])) then
attributes[splitString[0]] << splitString[1]
else
attributes[splitString[0]] = [splitString[1]]
end
end
puts "Putting #{attributes.size} attribute(s) into domain=[#{domain}], item=[#{item}] with replacement=[#{replace}]:"
attributes.each do |key,value|
puts " #{key}=[#{value.join(",")}]"
end
sdb = RightAws::SdbInterface.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
sdb.put_attributes(domain, item, attributes, replace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment