#!/usr/bin/env ruby require 'rubygems' require 'aws/s3' require 'find' # CONSTANTS # ACCESS_KEY_FILE = '/home/vagrant/.s3/keys' START_DIR = ARGV[0] BUCKET = ARGV[1] S3_ACCESS_KEY = 'AKIAIFBP7PVF6WBEGLGA' S3_SECRET_ACCESS_KEY = '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' # METHODS def check_params if ARGV.size != 2 && ARGV.size != 3 puts "Wrong number of arguments (#{ARGV.size} for 2)" exit end #if if ARGV.size == 3 && ARGV[2] == "true" @raked = true else @raked = false end #if if !FileTest.directory?(START_DIR) puts "#{START_DIR} is not a directory." exit end #if begin found = AWS::S3::Bucket.find(BUCKET) puts "#{BUCKET} is a valid bucket :)" unless @raked rescue puts "#{BUCKET} is not a valid bucket." exit end end #check_params def get_keys # lines = IO.readlines ACCESS_KEY_FILE # @access_key = lines.first.strip # @secret_access_key = lines.last.strip @access_key = S3_ACCESS_KEY @secret_access_key = S3_SECRET_ACCESS_KEY end #get_keys def establish_s3_connection get_keys @s3_connection = AWS::S3::Base.establish_connection!( :access_key_id => @access_key, :secret_access_key => @secret_access_key ) end #establish_s3_connection def upload(file) puts "Uploading #{file} to bucket #{BUCKET}..." unless @raked # Upload the given file AWS::S3::S3Object.store( file, open( file ), BUCKET, :access => :public_read ) # display the URL of the file just uploaded puts AWS::S3::S3Object.url_for((file), BUCKET)[/[^?]+/] puts unless @raked end #upload def upload_files # Get list of files do be added number_of_files = 0 paths = [] Find.find(START_DIR) do |path| if FileTest.directory?(path) next else paths.push(path) puts path unless @raked number_of_files += 1 end end if !@raked puts "Would you like to upload these #{number_of_files} files to the #{BUCKET} bucket? (y/n)" choice = STDIN.gets.strip.downcase else choice = 'y' end #if if choice == 'y' puts "Uploading #{number_of_files} files." paths.each do |path| upload path end #do puts "#{number_of_files} files uploaded." else puts "Exiting." end #if end #upload_files establish_s3_connection check_params upload_files