Module: S3Uploader

Defined in:
lib/s3_uploader/version.rb,
lib/s3_uploader/s3_uploader.rb

Constant Summary collapse

VERSION =
"0.1.3"
KILO_SIZE =
1024.0
BLOCK_SIZE =
1024 * 1024

Class Method Summary collapse

Class Method Details

.upload_directory(source, bucket, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
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
62
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
# File 'lib/s3_uploader/s3_uploader.rb', line 5

def self.upload_directory(source, bucket, options = {})
  options = {
    :destination_dir => '',
    :threads => 5,
    :s3_key => ENV['S3_KEY'],
    :s3_secret => ENV['S3_SECRET'],
    :public => false,
    :region => 'us-east-1',
    :metadata => {},
    :path_style => false,
    :regexp => /.*/,
    :gzip => false,
    :gzip_working_dir => nil,
    :time_range => Time.at(0)..(Time.now + (60 * 60 * 24))
  }.merge(options)

  log = options[:logger] || Logger.new(STDOUT)

  raise 'Source must be a directory' unless File.directory?(source)


  if options[:gzip]
    if options[:gzip_working_dir].nil?
      raise 'gzip_working_dir required when using gzip'
    else
      source_dir = source.end_with?('/') ? source : [ source, '/'].join
      gzip_working_dir = options[:gzip_working_dir].end_with?('/') ?
                            options[:gzip_working_dir] : [ options[:gzip_working_dir], '/'].join

      if gzip_working_dir.start_with?(source_dir)
        raise 'gzip_working_dir may not be located within source-folder'
      end
    end

    options[:gzip_working_dir] = options[:gzip_working_dir].chop if options[:gzip_working_dir].end_with?('/')
  end


  if options[:connection]
    connection = options[:connection]
  else
    raise "Missing access keys" if options[:s3_key].nil? || options[:s3_secret].nil?

    connection = Fog::Storage.new({
        :provider => 'AWS',
        :aws_access_key_id => options[:s3_key],
        :aws_secret_access_key => options[:s3_secret],
        :region => options[:region],
        :path_style => options[:path_style]
    })
  end

  source = source.chop if source.end_with?('/')

  if options[:destination_dir] != '' && !options[:destination_dir].end_with?('/')
    options[:destination_dir] = "#{options[:destination_dir]}/"
  end
  total_size = 0
  files = Queue.new

  Dir.glob("#{source}/**/*").select { |f| !File.directory?(f) }.each do |f|
    if File.basename(f).match(options[:regexp]) && options[:time_range].cover?(File.mtime(f))
      if options[:gzip] && File.extname(f) != '.gz'
        dir, base = File.split(f)
        dir       = dir.sub(source, options[:gzip_working_dir])
        gz_file   = "#{dir}/#{base}.gz"

        FileUtils.mkdir_p(dir) unless File.directory?(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 = []
  options[: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.gsub(source, '').gsub(options[:gzip_working_dir].to_s, '')[1..-1]
          dest = "#{options[:destination_dir]}#{key}"
          body = File.open(file)
          log.info("[#{Thread.current["file_number"]}/#{total_files}] Uploading #{key} to s3://#{bucket}/#{dest}")

          directory.files.create(
            :key    => dest,
            :body   => body,
            :public => options[:public],
            :metadata => options[: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
  log.info("Uploaded %d (%.#{0}f KB) in %d:%04.2f" % [total_files, total_size / KILO_SIZE, mins.to_i, secs])

end