# 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 example in examples/s3.yaml - # you need to set your keys and the name of the default_bucket to use for storing files; # then change the config_file line below to set the path to your config. file. # # NB 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) # set to the path to your s3.yaml file config_file = '/home/ell/.s33r' require 'yaml' require 'net/smtp' require 'time' require 'parsedate' # load the S33r libraries require File.join(File.dirname(__FILE__), '..', '..', 'lib', 's33r') filename = ARGV[0] to_email = ARGV[1] # load config. file access_key, secret_key, options, custom = S33r::Client.load_config(config_file) # bucket name to work with bucket = options[:default_bucket] puts "Using bucket: #{bucket}" # email addresses to_email = options[:to_email] from_email = options[:from_email] # expires can be a date/time string or 'forever'; # if not set, defaults to 15 minutes expires = S33r.parse_expiry(options[:default_expires]) # check for bucket if !bucket require 'readline' bucket = Readline.readline('No bucket found; please enter name for new bucket: ', true) client = S33r::Client.new(access_key, secret_key) response = client.create_bucket(bucket) if response.ok? puts 'Created new bucket' else puts 'Could not create bucket (HTTP problem)' exit end end options[:default_bucket] = bucket # 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 S33r::NamedBucket.new(access_key, secret_key, options) do |c| response = c.put_file(filename) url = c.s3_authenticated_url(filename, expires) 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" pretty_expires = Time.at(expires).httpdate puts "Available at URL:" puts url puts "This link will expire at %s" % pretty_expires 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}\n\n" + "This link will expire at %s" % pretty_expires Net::SMTP.start('localhost') do |smtp| smtp.send_message message, from_email, to_email end end else puts "File transfer failed with response code #{response.code}" end end