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



86
87
88
89
90
91
# File 'lib/s3up.rb', line 86

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

#file_changed(key, file) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/s3up.rb', line 57

def file_changed( key, file )
  stat = File.stat( file )
  key.refresh( true )
  if stat.size != key.size
    puts "file changed" if @options[ :verbose ]
    true
  else
    puts "file not changed" if @options[ :verbose ]
    false
  end
end

#generate_manifestObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/s3up.rb', line 104

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

#initObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/s3up.rb', line 17

def init
  keys = @command_line_options.keys.select { | k | k.match( /_given/ ) }.map { | k | k[ 0 .. -7 ] }
  init_options = @command_line_options.select {|k| keys.include?( k.to_s ) }
  init_options.delete( :init )
  options = {
    :id => 'AWS_ACCESS_KEY_ID',
    :key => 'AWS_SECRET_ACCESS_KEY',
    :bucket => 's3up.test.bucket',
    :prefix => 'key_prefix/',
    :types => 'jpg,png,mp3,json',
    :threads => 10
  }
  options.merge!( init_options )
  File.open( './.s3uprc', 'w' ) do | file |
    options.each do | key, value |
      file.puts "#{ key }: #{ value }"
    end
  end
end

#load_options(file) ⇒ Object



41
42
43
44
45
46
# File 'lib/s3up.rb', line 41

def load_options( file )
  if File.exists?( File.expand_path( file ) )
    options = YAML.load_file( File.expand_path( file ) )
    overwrite_options( options ) if options
  end
end

#overwrite_options(options) ⇒ Object



37
38
39
# File 'lib/s3up.rb', line 37

def overwrite_options( options )
  options.each { | key, value | @options[ key.to_sym ] = value if value }
end

#s3Object



69
70
71
# File 'lib/s3up.rb', line 69

def s3
  @s3 ||= Aws::S3.new( @options[ :id ], @options[ :key ], :connection_mode  => :per_thread )
end

#setup_options(options) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/s3up.rb', line 48

def setup_options( options )
  @command_line_options = options
  @options = {}
  load_options( '~/.s3uprc' )
  load_options( './.s3uprc' )
  overwrite_options( options )
  puts @options if @options[ :verbose ]
end

#uploadObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/s3up.rb', line 93

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/s3up.rb', line 73

def upload_file( bucket_name, filename, key_name = filename )
  bucket = s3.bucket( bucket_name )
  key = Aws::S3::Key.create( bucket ,key_name )
  if @options[ :force ] || file_changed( key, filename )
    key.put( File.open( filename ).read, 'public-read' )
    puts "uploaded: #{ key.full_name }"
  else 
    puts "skipped: #{ key.full_name }"
  end
rescue Exception => e
  puts e
end