Class: S3Uploader::Uploader
- Inherits:
-
Object
- Object
- S3Uploader::Uploader
- Defined in:
- lib/s3_uploader/s3_uploader.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Uploader
constructor
A new instance of Uploader.
- #upload(source_dir, bucket) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Uploader
Returns a new instance of Uploader.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/s3_uploader/s3_uploader.rb', line 18 def initialize( = {}) = { :destination_dir => '', :threads => DEFAULT_THREADS_NUMBER, :s3_key => ENV['S3_KEY'], :s3_secret => ENV['S3_SECRET'], :public => false, :region => DEFAULT_AWS_REGION, :metadata => {}, :path_style => false, :regexp => nil, :gzip => false, :gzip_working_dir => nil, :time_range => Time.at(0)..(Time.now + (60 * 60 * 24)) }.merge() @logger = [:logger] || Logger.new(STDOUT) if [:gzip] && [:gzip_working_dir].nil? raise 'gzip_working_dir required when using gzip' end if [:connection] @connection = [:connection] else if [:s3_key].nil? || [:s3_secret].nil? raise "Missing access keys" end @connection = Fog::Storage.new({ :provider => 'AWS', :aws_access_key_id => [:s3_key], :aws_secret_access_key => [:s3_secret], :region => [:region], :path_style => [:path_style] }) end if ![:destination_dir].to_s.empty? && ![:destination_dir].end_with?('/') [:destination_dir] << '/' end end |
Instance Attribute Details
#logger=(value) ⇒ Object (writeonly)
Sets the attribute logger
16 17 18 |
# File 'lib/s3_uploader/s3_uploader.rb', line 16 def logger=(value) @logger = value end |
Instance Method Details
#upload(source_dir, bucket) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/s3_uploader/s3_uploader.rb', line 63 def upload(source_dir, bucket) raise 'Source directory is requiered' if source_dir.to_s.empty? source = source_dir.dup source << '/' unless source.end_with?('/') raise 'Source must be a directory' unless File.directory?(source) gzip_working_dir = [:gzip_working_dir] if [:gzip] && !gzip_working_dir.to_s.empty? gzip_working_dir << '/' unless gzip_working_dir.end_with?('/') if gzip_working_dir.start_with?(source) raise 'gzip_working_dir may not be located within source-folder' end end total_size = 0 files = Queue.new regexp = [:regexp] Dir.glob(File.join(source, '**/*')) .select { |f| !File.directory?(f) }.each do |f| if (regexp.nil? || File.basename(f).match(regexp)) && [:time_range].cover?(File.mtime(f)) if [:gzip] && File.extname(f) != '.gz' dir, base = File.split(f) dir = dir.sub(source, gzip_working_dir) gz_file = File.join(dir, [ base, '.gz' ].join) @logger.info("Compressing #{f}") FileUtils.mkdir_p(dir) Zlib::GzipWriter.open(gz_file) do |gz| gz.mtime = File.mtime(f) gz.orig_name = f File.open(f, 'rb') do |fi| while (block_in = fi.read(BLOCK_SIZE)) do gz.write block_in end end end files << gz_file total_size += File.size(gz_file) else files << f total_size += File.size(f) end end end directory = @connection.directories.new(:key => bucket) start = Time.now total_files = files.size file_number = 0 @mutex = Mutex.new threads = [] [:threads].times do |i| threads[i] = Thread.new do until files.empty? @mutex.synchronize do file_number += 1 Thread.current["file_number"] = file_number end file = files.pop rescue nil if file key = file.sub(source, '').sub(gzip_working_dir.to_s, '') dest = [ [:destination_dir], key ].join body = File.open(file) @logger.info(["[", Thread.current["file_number"], "/", total_files, "] Uploading ", key, " to s3://#{bucket}/#{dest}" ].join) directory.files.create( :key => dest, :body => body, :public => [:public], :metadata => [:metadata] ) body.close end end end end threads.each { |t| t.join } finish = Time.now elapsed = finish.to_f - start.to_f mins, secs = elapsed.divmod 60.0 @logger.info("Uploaded %d (%.#{0}f KB) in %d:%04.2f" % [total_files, total_size / KILO_SIZE, mins.to_i, secs]) end |