#!/usr/bin/env ruby # Upload a bunch of files to S3 with an index page; # send the index page to your friends! # # Note that your filename pattern will not upload directories, only files; # and that it doesn't yet recursively descend into directories require 'rubygems' require 'readline' require File.join(File.dirname(__FILE__), '../../lib/s33r') include S33r include Readline # AMAZON S3 KEYS!!! accesskey = 'accesskey' secretaccesskey = 'secretaccesskey' if 'accesskey' == accesskey config, _ = S33r.load_config('../fores33r/config/s3.yaml') accesskey = config[:access] secretaccesskey = config[:secret] end puts "****************************" # Get bucket name from the user. bucket_name = "elliotsmith-marvellous-bucket" entered_bucket = readline("Enter name of bucket to use (will be created if it doesn't exist) \n\ Press Enter to use default [#{bucket_name}]: ", true) bucket_name = entered_bucket if entered_bucket.size > 0 puts "" # Get a list of files to upload from the user. pattern = readline("Enter a pattern which matches the files you want to upload\n\ e.g. /home/pete/photos/*.jpg will upload all *.jpg files in /home/pete/photos\n\ (basically, its a Ruby glob pattern which will return files):\n", true) files = Dir[pattern] # Get an S3 client. client = Client.new(:access => accesskey, :secret => secretaccesskey, :persistent => true) # Create the bucket if it doesn't exist. unless client.bucket_exists?(bucket_name) puts "Creating new bucket #{bucket_name}" client.create_bucket(bucket_name) puts "" end # Start creating the download server. puts "****************************" puts "Your \"instant download server\" will be ready in a jiffy" puts "" # Get Bucket instance. bucket = client.get_bucket(bucket_name) # We're going to create an HTML listing from the list of files # (yes, I know it's broken HTML), then upload it. Self-reflexive! html = "

Files in #{bucket_name}

\n" # Put the files onto the server, adding an authenticated link for each # file as it is uploaded. # Create authenticated URLs which never expire (well, expire in 20 years time). url_options = {:expires => :far_flung_future} puts "Sending files to S3" files.each do |f| if File.stat(f).file? puts "#{f}" ok = bucket.put_file(f) unless ok puts "There were problems uploading your files (perhaps your access keys are wrong)" exit end html << "

f, :authenticated => true)) + "\">" + f + "

\n" end end puts "" # Put the list on the server as an index page. puts "Creating index page..." bucket.put(html, :key => 'index.html', :content_type => 'text/html') puts "Done!" puts "" # Display the protected URL for the index page. puts "Here's the URL for your brand spanking new \"download server\":\n\n" + bucket.url(url_options.merge(:key => 'index.html', :authenticated => true))