Class: S3up

Inherits:
Object
  • Object
show all
Defined in:
lib/s3up.rb

Instance Method Summary collapse

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( options = {} )
  setup_options( options )
  @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_manifestObject



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 load_options( file )
  if File.exists?( File.expand_path( file ) )
    options = YAML.load_file( File.expand_path( file ) )
    overwrite_options( options )
  end
end

#overwrite_options(options) ⇒ Object



17
18
19
# File 'lib/s3up.rb', line 17

def overwrite_options( options )
  options.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 setup_options( options )
  @options = {}
  load_options( '~/.s3uprc' )
  load_options( './.s3uprc' )
  overwrite_options( options )
  puts @options if @options[ :verbose ]
end

#uploadObject



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