Class: S3up
- Inherits:
-
Object
- Object
- S3up
- Defined in:
- lib/s3up.rb
Instance Method Summary collapse
- #create_bucket(bucket_name) ⇒ Object
- #generate_manifest ⇒ Object
-
#initialize(options = {}) ⇒ S3up
constructor
A new instance of S3up.
- #load_options(file) ⇒ Object
- #overwrite_options(options) ⇒ Object
- #setup_options(options) ⇒ Object
- #upload ⇒ Object
- #upload_file(bucket_name, filename, key_name = filename) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ S3up
Returns a new instance of S3up.
10 11 12 13 14 15 |
# File 'lib/s3up.rb', line 10 def initialize( = {} ) ( ) @work_queue = WorkQueue.new( @options[ :threads ] ) glob = "#{ @options[ :folder ] }/*.{#{ @options[ :types ] }}" @files = Dir.glob( glob, File::FNM_CASEFOLD ) end |
Instance Method Details
#create_bucket(bucket_name) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/s3up.rb', line 46 def create_bucket( bucket_name ) s3 = Aws::S3.new( @options[ :id ], @options[ :key ] ) bucket = s3.bucket( bucket_name, true, 'public-read' ) rescue Exception => e puts e end |
#generate_manifest ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/s3up.rb', line 64 def generate_manifest items = @files.map do | file | stat = File.stat( file ) { :uri => "http://#{ @options[ :bucket ] }.s3.amazonaws.com/#{ @options[ :prefix ] }#{ file }", :size => stat.size, :type => MIME::Types.of( file ).first } end File.open( @options[ :manifest ], 'w' ) do | file | file.puts( JSON.pretty_generate( { :objects => items } ) ) end end |
#load_options(file) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/s3up.rb', line 21 def ( file ) if File.exists?( File.( file ) ) = YAML.load_file( File.( file ) ) ( ) end end |
#overwrite_options(options) ⇒ Object
17 18 19 |
# File 'lib/s3up.rb', line 17 def ( ) .each { | key, value | @options[ key.to_sym ] = value if value } end |
#setup_options(options) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/s3up.rb', line 28 def ( ) @options = {} ( '~/.s3uprc' ) ( './.s3uprc' ) ( ) puts @options if @options[ :verbose ] end |
#upload ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/s3up.rb', line 53 def upload create_bucket( @options[ :bucket ] ) @files.each do | file | @work_queue.enqueue_b { dest_key = "#{ @options[ :prefix ] }#{ file }" upload_file( @options[ :bucket ], file, dest_key ) } end @work_queue.join end |
#upload_file(bucket_name, filename, key_name = filename) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/s3up.rb', line 36 def upload_file( bucket_name, filename, key_name = filename ) s3 = Aws::S3.new( @options[ :id ], @options[ :key ], :connection_mode => :per_thread ) bucket = s3.bucket( bucket_name ) key = Aws::S3::Key.create( bucket ,key_name ) key.put( File.open( filename ).read, 'public-read' ) puts "uploaded: #{ key.full_name }" if @options[ :verbose ] rescue Exception => e puts e end |