Skip to content

Instantly share code, notes, and snippets.

@ox
Created December 17, 2011 20:36
Show Gist options
  • Save ox/1491311 to your computer and use it in GitHub Desktop.
Save ox/1491311 to your computer and use it in GitHub Desktop.
A ruby-driven utility for light interaction with an amazon S3 server
#!/usr/bin/env ruby
require 'aws/s3'
def help
puts "S3Console usage:\n"
puts "s3con <bucket>"
exit
end
help if ARGV.empty?
bucket = ARGV.first
AWS::S3::Base.establish_connection!(
:access_key_id => '',
:secret_access_key => ''
)
include AWS::S3
puts bucket
while true
print "> "
cmd = STDIN.gets.chomp.split ' '
case cmd[0]
when 'rm'
puts " => #{S3Object.delete cmd[1], bucket}"
when 'ls'
Bucket.find(bucket).objects.sort {|x,y| x.last_modified <=> y.last_modified}.each do |o|
puts "#{o.key.ljust(15)} \033[1;31m#{(o.size/1024).to_s.rjust(7)}\033[0m kB #{o.content_type.to_s.rjust(10)} #{o.last_modified.to_s.ljust(25)}"
end
when 'connect'
bucket = cmd[1]
puts " => bucket = #{cmd[1]}"
when 'bucket'
puts " => #{bucket}"
when 'open'
`open #{S3Object.url_for(cmd[1], bucket)}`
when 'upload'
if cmd.length >= 2 && File.exist?(cmd[1])
begin
S3Object.save cmd[2] || cmd[1], open(cmd[1]), bucket
puts " => true"
rescue
puts " => false\n[error uploading to S3]"
end
else
if cmd.length >= 2 && !File.exist?(cmd[1] || "")
puts "#{cmd[1]} doesn't exist"
else
puts "usage: upload <file/path> [name_of_file]"
end
end
when 'help'
puts " commands:\n rm <file>\n ls\n connect <bucket>\n bucket\n upload <file> [new file name]\n quit"
when 'quit'
break
when 'exit'
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment