Class: S3Enumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/envoi/utils/s3_enumerator.rb

Constant Summary collapse

HUMAN_READABLE_SHORT_SUFFIX =
%w(Bytes KB MB GB TB PB EB ZB YB)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ S3Enumerator

Returns a new instance of S3Enumerator.



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
# File 'lib/envoi/utils/s3_enumerator.rb', line 17

def initialize(args = { })
  aws_access_key_id = args[:aws_access_key_id]
  aws_secret_access_key = args[:aws_secret_access_key]
  aws_region = args[:aws_region]

  aws_config = {}
  aws_config[:credentials] = (aws_access_key_id || aws_secret_access_key) ?
                                 Aws::Credentials.new(aws_access_key_id, aws_secret_access_key) :
                                 Aws::SharedCredentials.new(profile_name: args[:aws_profile])
  aws_config[:region] = aws_region if aws_region
  Aws.config.update(aws_config) unless aws_config.empty?

  @preview_only = args[:preview_only]
  @should_show_summary = args.fetch(:show_summary, preview_only)

  @limit = args[:limit]
  @limit = @limit.to_i if @limit

  @max_keys = 1000
  @max_keys = @limit if @limit && @limit < @max_keys

  @s3_bucket_name = args[:s3_bucket_name] || args[:bucket_name]
  @s3_object_key_prefix = args[:s3_object_key_prefix] || args[:object_key_prefix] || ''
  @s3_object_key_prefix = @s3_object_key_prefix[1..-1] if @s3_object_key_prefix.start_with?('/')

  @s3 = Aws::S3::Client.new
  @s3_bucket_region = s3.get_bucket_location(bucket: @s3_bucket_name).location_constraint
  @s3_bucket_region = 'us-east-1' if @s3_bucket_region.empty?

  @objects = []
  @files = []
  @ignored = []
  @directories = []
  @objects_by_ext = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#directoriesObject (readonly)

Returns the value of attribute directories.



13
14
15
# File 'lib/envoi/utils/s3_enumerator.rb', line 13

def directories
  @directories
end

#filesObject (readonly)

Returns the value of attribute files.



13
14
15
# File 'lib/envoi/utils/s3_enumerator.rb', line 13

def files
  @files
end

#largest_fileObject (readonly)

Returns the value of attribute largest_file.



13
14
15
# File 'lib/envoi/utils/s3_enumerator.rb', line 13

def largest_file
  @largest_file
end

#limitObject (readonly)

Returns the value of attribute limit.



11
12
13
# File 'lib/envoi/utils/s3_enumerator.rb', line 11

def limit
  @limit
end

#max_keysObject (readonly)

Returns the value of attribute max_keys.



11
12
13
# File 'lib/envoi/utils/s3_enumerator.rb', line 11

def max_keys
  @max_keys
end

#objectsObject (readonly)

Returns the value of attribute objects.



7
8
9
# File 'lib/envoi/utils/s3_enumerator.rb', line 7

def objects
  @objects
end

#objects_by_extObject (readonly)

Returns the value of attribute objects_by_ext.



13
14
15
# File 'lib/envoi/utils/s3_enumerator.rb', line 13

def objects_by_ext
  @objects_by_ext
end

#objects_by_storage_classObject (readonly)

Returns the value of attribute objects_by_storage_class.



13
14
15
# File 'lib/envoi/utils/s3_enumerator.rb', line 13

def objects_by_storage_class
  @objects_by_storage_class
end

#preview_onlyObject (readonly)

Returns the value of attribute preview_only.



9
10
11
# File 'lib/envoi/utils/s3_enumerator.rb', line 9

def preview_only
  @preview_only
end

#s3Object (readonly)

Returns the value of attribute s3.



5
6
7
# File 'lib/envoi/utils/s3_enumerator.rb', line 5

def s3
  @s3
end

#s3_bucket_nameObject (readonly)

Returns the value of attribute s3_bucket_name.



5
6
7
# File 'lib/envoi/utils/s3_enumerator.rb', line 5

def s3_bucket_name
  @s3_bucket_name
end

#s3_bucket_regionObject (readonly)

Returns the value of attribute s3_bucket_region.



5
6
7
# File 'lib/envoi/utils/s3_enumerator.rb', line 5

def s3_bucket_region
  @s3_bucket_region
end

#s3_object_key_prefixObject (readonly)

Returns the value of attribute s3_object_key_prefix.



5
6
7
# File 'lib/envoi/utils/s3_enumerator.rb', line 5

def s3_object_key_prefix
  @s3_object_key_prefix
end

#should_show_summaryObject (readonly)

Returns the value of attribute should_show_summary.



9
10
11
# File 'lib/envoi/utils/s3_enumerator.rb', line 9

def should_show_summary
  @should_show_summary
end

#total_bytesObject (readonly)

Returns the value of attribute total_bytes.



13
14
15
# File 'lib/envoi/utils/s3_enumerator.rb', line 13

def total_bytes
  @total_bytes
end

Instance Method Details

#concat_objects(objects, resp) ⇒ Object



53
54
55
56
# File 'lib/envoi/utils/s3_enumerator.rb', line 53

def concat_objects(objects, resp)
  objects.concat resp.contents
  objects
end

#grouped_data_to_table(collection, key_name) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/envoi/utils/s3_enumerator.rb', line 143

def grouped_data_to_table(collection, key_name)
  table_data =  [ [ key_name, 'Count', 'Short Bytes', 'Total Bytes' ] ]
  collection.each do |key, objects|
    group_total_size = objects.reduce(0) { |s, o| s + o.size }
    human_readable_group_total_size = humanize_number(group_total_size)
    human_readable_group_total_size_short = human_readable_bytes_short(human_readable_group_total_size)
    row_values = [ key, humanize_number(objects.length), human_readable_group_total_size_short, human_readable_group_total_size ]
    table_data << row_values
  end
  table_data
end

#human_readable_bytes_short(human_readable_number) ⇒ Object



135
136
137
# File 'lib/envoi/utils/s3_enumerator.rb', line 135

def human_readable_bytes_short(human_readable_number)
  "#{human_readable_number.split(',').first} #{HUMAN_READABLE_SHORT_SUFFIX[human_readable_number.count(',')]}"
end

#humanize_number(number) ⇒ Object



139
140
141
# File 'lib/envoi/utils/s3_enumerator.rb', line 139

def humanize_number(number)
  number.to_s.chars.to_a.reverse.each_slice(3).map(&:join).join(',').reverse
end


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
# File 'lib/envoi/utils/s3_enumerator.rb', line 104

def print_table(data, options = { })
  first_row = data.first
  table = first_row.is_a?(Hash) ? [first_row.keys] + data.map(&:values) : data

  widths = []
  table.each do |line|
    line.each_with_index do |col, idx|
      cur_col_width = widths[idx]
      if cur_col_width
        col_len = col.to_s.length
        widths[idx] = col_len if col_len > cur_col_width
      else
        widths[idx] = col.to_s.length
      end
    end
  end

  # header separator
  separator_ary = widths.map { |n| '-' * n }
  table.insert(1, separator_ary)
  table.insert(-2, separator_ary) if options[:has_totals]

  format = widths.collect { |n| "%-#{n}s"}.join(' | ')
  table.each { |line| printf "| #{format} |\n", *line }
end

#process_files(args = { }) ⇒ Object



180
181
182
# File 'lib/envoi/utils/s3_enumerator.rb', line 180

def process_files(args = { })
  process_objects(files, { total_bytes: total_bytes })
end

#process_objects(objects, args = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/envoi/utils/s3_enumerator.rb', line 184

def process_objects(objects, args = {})
  return objects unless block_given?
  return objects.map { |object| yield object, self } if args[:quiet]

  bytes_remaining = args[:total_bytes] || objects.reduce(0) { |s, o| s + o.size }
  _total_object_count = objects.length
  counter = 0
  objects.each do |object|
    counter += 1
    human_readable_bytes_remaining = humanize_number(bytes_remaining)
    human_readable_bytes_remaining_short = human_readable_bytes_short(human_readable_bytes_remaining)
    human_readable_object_bytes = humanize_number(object.size)
    human_readable_object_bytes_short = human_readable_bytes_short(human_readable_object_bytes)
    puts "Processing #{humanize_number(counter)} of #{humanize_number(_total_object_count)} #{human_readable_object_bytes} (#{human_readable_object_bytes_short}) of #{human_readable_bytes_remaining} (#{human_readable_bytes_remaining_short})  #{object.key}"
    yield object, self if block_given?
    bytes_remaining -= object.size
  end
end

#retrieve_objects(&block) ⇒ Object



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
# File 'lib/envoi/utils/s3_enumerator.rb', line 62

def retrieve_objects(&block)
  @objects = []

  resp = s3.list_objects_v2(bucket: s3_bucket_name, prefix: s3_object_key_prefix, max_keys: max_keys)
  loop do
    concat_objects(objects, resp)
    puts objects.length
    break if !resp.next_page? || (limit && total_object_count >= limit)
    resp = resp.next_page
  end
  @objects = objects.first(limit) if limit

  @files = []
  @directories = []
  @objects_by_ext = Hash.new { |h, k| h[k] = [] }
  @objects_by_storage_class = Hash.new { |h, k| h[k] = [] }
  #
  @total_bytes = 0
  @largest_file = nil
  objects.each do |object|
    if object.key.end_with?('/')
      @directories << object
    else
      filename = File.basename(object.key)
      should_ignore = filename.start_with?('.')
      if should_ignore
        @ignored << object
      else
        @files << object
        @total_bytes += object.size
        @largest_file = object unless @largest_file && @largest_file.size > object.size
        filename_ext = File.extname(filename).downcase
        objects_by_ext[filename_ext] << object
        objects_by_storage_class[object.storage_class] << object
      end
    end

    yield object, self if block_given?
  end
end

#run(&block) ⇒ Object



130
131
132
133
# File 'lib/envoi/utils/s3_enumerator.rb', line 130

def run(&block)
  retrieve_objects(&block)
  show_summary if @should_show_summary
end

#show_summaryObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/envoi/utils/s3_enumerator.rb', line 155

def show_summary
  objects_by_ext_table_data = grouped_data_to_table(objects_by_ext, 'File Ext')
  objects_by_storage_class_table_data = grouped_data_to_table(objects_by_storage_class, 'Storage Class')

  human_readable_total_bytes = humanize_number(total_bytes)
  human_readable_total_bytes_short = human_readable_bytes_short(human_readable_total_bytes)
  row_data = [ 'TOTAL', humanize_number(objects.length), human_readable_total_bytes_short,  human_readable_total_bytes]
  objects_by_ext_table_data << row_data
  objects_by_storage_class_table_data << row_data

  puts "\n\n--- Summary ---"
  puts "Bucket Name: #{s3_bucket_name}"
  puts "Bucket Region: #{s3_bucket_region}"
  puts "Object Key Prefix: #{s3_object_key_prefix}"
  puts "Total Objects: #{humanize_number(total_object_count)}"
  puts "Total Directories: #{humanize_number(directories.length)}"
  puts "Total Files: #{humanize_number(files.length)}"
  puts "Total Size (in bytes): #{human_readable_total_bytes} (#{human_readable_total_bytes_short})"
  puts "Largest File: #{largest_file}"
  puts "\n"
  print_table(objects_by_ext_table_data, { has_totals: true })
  puts "\n"
  print_table(objects_by_storage_class_table_data, { has_totals: true })
end

#total_object_countObject



58
59
60
# File 'lib/envoi/utils/s3_enumerator.rb', line 58

def total_object_count
  objects.length
end