Class: S3up
- Inherits:
-
Object
- Object
- S3up
- Defined in:
- lib/s3up.rb
Instance Method Summary collapse
- #create_bucket(bucket_name) ⇒ Object
- #file_changed(key, file) ⇒ Object
- #generate_manifest ⇒ Object
- #init ⇒ Object
-
#initialize(options = {}) ⇒ S3up
constructor
A new instance of S3up.
- #load_options(file) ⇒ Object
- #overwrite_options(options) ⇒ Object
- #s3 ⇒ 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
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_manifest ⇒ Object
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 |
#init ⇒ Object
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 ] } = @command_line_options.select {|k| keys.include?( k.to_s ) } .delete( :init ) = { :id => 'AWS_ACCESS_KEY_ID', :key => 'AWS_SECRET_ACCESS_KEY', :bucket => 's3up.test.bucket', :prefix => 'key_prefix/', :types => 'jpg,png,mp3,json', :threads => 10 } .merge!( ) File.open( './.s3uprc', 'w' ) do | file | .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 ( file ) if File.exists?( File.( file ) ) = YAML.load_file( File.( file ) ) ( ) if end end |
#overwrite_options(options) ⇒ Object
37 38 39 |
# File 'lib/s3up.rb', line 37 def ( ) .each { | key, value | @options[ key.to_sym ] = value if value } end |
#s3 ⇒ Object
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 ( ) @command_line_options = @options = {} ( '~/.s3uprc' ) ( './.s3uprc' ) ( ) puts @options if @options[ :verbose ] end |
#upload ⇒ Object
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 |