# SIMPLE COMMAND LINE S3 CLIENT # You need an Amazon S3 account to use this: get one at http://aws.amazon.com/ # # Create a config. file called based on the config.yml example in this directory; # then change the config_file setting below to set the path to your config. file. # You will be prompted to create a bucket if your setting for bucket in the config. # file is blank. # # Note that this script doesn't give you any means of deleting anything; you will # need to write your own program to do this (I use my own s33r server, # which I am currently re-implementing to work with this library). # # Call this script with two arguments: # filename: file to upload; the file is uploaded to S3 and assigned a key # based on filename supplied (including path) # to_email: email address to send to (optional) require 'yaml' require 'rubygems' require_gem 's33r' require 'net/smtp' filename = ARGV[0] to_email = ARGV[1] config_file = '/home/ell/.s33r' if '/path/to/your/config/file' == config_file puts 'Please set the config_file variable to the path to your config. file' exit end # load config. file options = YAML::load_file(config_file) access_key = options['access_key'] secret_key = options['secret_key'] from_email = options['from_email'] to_email ||= options['to_email'] bucket = options['bucket'] # check for bucket if !bucket require 'readline' bucket = Readline.readline('No bucket found; please enter name for new bucket: ', true) client = S3::Client.new(access_key, secret_key) response = client.create_bucket(bucket, client.canned_acl_header('public-read')) if response.ok? puts 'Created new bucket' else puts 'Could not create bucket (HTTP problem)' exit end end # estimate upload time filesize = FileTest.size(filename) filesize_mb = (filesize / (1000*1000)).to_i secs_per_mb = 12 str = "Transferring file; size %d bytes" % filesize str += " (about %d Mb)" % filesize_mb if filesize_mb > 0 str += "\nUploading normally takes at least %d seconds per Mb\n" % secs_per_mb if filesize_mb > 0 str += "So this should take about %d seconds" % (filesize_mb * secs_per_mb) else str += "So this should be done in no time" end puts str # time the put start_time_secs = Time.now.to_i # a client pointing at the specified bucket S3::NamedBucket.new(access_key, secret_key, bucket, :public_contents => true) do |client| response = client.put_file(filename) end time_taken_secs = Time.now.to_i - start_time_secs puts "Aah, it appears to have taken %d seconds" % time_taken_secs # post-put report if response.ok? puts "File #{filename} transferred OK" url = File.join("http://", S3::HOST, client.bucket_name, '/') + filename puts "Available at URL:" puts url if to_email message = "From:#{from_email}\r\nTo:#{to_email}\r\nSubject:You were sent a file\r\n\r\nFetch it from\n#{url}" Net::SMTP.start('localhost') do |smtp| smtp.send_message message, from_email, to_email end end else puts 'File transfer failed' end