bin/s3cli.rb in s33r-0.2 vs bin/s3cli.rb in s33r-0.3

- old
+ new

@@ -1,9 +1,9 @@ # 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; +# Create a config. file called based on the example in test/files/namedbucket_config.yml; # 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 @@ -14,32 +14,36 @@ # 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' require 'time' require 'parsedate' +require File.join(File.dirname(__FILE__), '../lib/s33r') + filename = ARGV[0] to_email = ARGV[1] # set to the path to your config. file config_file = '/home/ell/.s33r' # 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'] +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 = custom[:to_email] +from_email = custom[:from_email] + # expires can be a date/time string or 'forever'; # if not set, defaults to 15 minutes -expires = options['default_expires'] +expires = options[:default_expires] base_expires = Time.now.to_i if 'forever' == expires # 20 years (same as forever in computer terms) expires = base_expires + (60 * 60 * 24 * 365.25 * 20).to_i elsif expires.is_a?(String) @@ -52,20 +56,22 @@ # 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) + 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 @@ -82,14 +88,16 @@ # 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) do |c| - response = c.put_file(filename, filename) - url = c.s3_authenticated_url(filename, expires) +S33r::NamedBucket.new(access_key, secret_key, options) do |c| + response = c.put_file(filename) + + url = c.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? @@ -108,8 +116,9 @@ Net::SMTP.start('localhost') do |smtp| smtp.send_message message, from_email, to_email end end else - puts 'File transfer failed' + puts "File transfer failed with response code #{response.code}" end -end + +end \ No newline at end of file