s3grep 0.1.5 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 516c9439c60a587b2c9d20b2bc5b9203036f4df390a0c5f23e4920516669ff42
4
- data.tar.gz: ee817321b9d23fdb6d4af1b31cf31ec4f0fb836dfde3216ac63b1225ad85810f
3
+ metadata.gz: d88e4a4103d7d4263d025c54f4a5be65349961caf01026b510b3f16ba056e23c
4
+ data.tar.gz: a60b92d47715a38252c9a903c2f95c11841fbaa876ef1f8ccee3a99d92c90d2f
5
5
  SHA512:
6
- metadata.gz: f0373f0c049d7fa1a07279cb727b4d7e8950d8da0d8fe7de83189b633152acf66c150feb4c16b59c9ed7c17bab38b135a6a9a83700c05b1e24a0b53aaf7824c4
7
- data.tar.gz: 3ec8bef5e1c31339d26bd0f45bd213f2cb9e2fd08f8372a20bbca391f8167af70081a90d6e613c315b0e175ff0c4af67c4c59edf9270f6b381787fa2829f8bb3
6
+ metadata.gz: 989e6da04b302d4fe68f4d8ed52c8f813d45ba359d7e270414b9602020d017380d1e611e50958fc5a4ee8edb6356e1c5a5243dd66d1182f899bc71825926e420
7
+ data.tar.gz: 89d12dd7fc507b998b4f1bdafa62b9a012650ed7aec71dd08df729ee0b2d215295e9eb8f02558f3122e0c50ee789192cfe53f5399d3b2d169cd975276750bb21
data/.gitignore CHANGED
@@ -55,3 +55,5 @@ build-iPhoneSimulator/
55
55
 
56
56
  # Used by RuboCop. Remote config files pulled in from inherit_from directive.
57
57
  # .rubocop-https?--*
58
+
59
+ *.csv
data/bin/s3info ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ BASE_DIR = Pathname.new(File.expand_path('..', __dir__))
5
+ $LOAD_PATH << "#{BASE_DIR}/lib"
6
+
7
+ require 'optparse'
8
+ require 's3grep'
9
+ require 'aws-sdk-s3'
10
+ require 'json'
11
+
12
+ s3_file = ARGV[0]
13
+ aws_s3_client = Aws::S3::Client.new
14
+ info = S3Grep::Directory.new(s3_file, aws_s3_client).info
15
+
16
+ stats = {
17
+ bucket: info.bucket,
18
+ base_prefix: info.base_prefix,
19
+ total_size: info.total_size,
20
+ num_files: info.num_files,
21
+ last_modified: info.last_modified,
22
+ newest_file: info.newest_file,
23
+ first_modified: info.first_modified,
24
+ first_file: info.first_file,
25
+ num_files_by_storage_class: info.num_files_by_storage_class,
26
+ total_size_by_storage_class: info.total_size_by_storage_class
27
+ }
28
+
29
+ print JSON.pretty_generate(stats) + "\n"
data/bin/s3report ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 's3grep'
5
+ require 'aws-sdk-s3'
6
+ require 'csv'
7
+
8
+ bucket_info = {}
9
+ aws_s3_client = Aws::S3::Client.new
10
+ aws_s3_client.list_buckets[:buckets].each do |bucket|
11
+ name = bucket[:name]
12
+ puts name
13
+
14
+ bucket_location = aws_s3_client.get_bucket_location(bucket: name)
15
+ aws_s3_client_region_specific =
16
+ if bucket_location[:location_constraint] == ''
17
+ aws_s3_client
18
+ else
19
+ Aws::S3::Client.new(region: bucket_location[:location_constraint])
20
+ end
21
+
22
+ info = S3Grep::Directory.new("s3://#{name}/", aws_s3_client_region_specific).info
23
+
24
+ bucket_info[name] = {
25
+ bucket: info.bucket,
26
+ creation_date: bucket[:creation_date],
27
+ total_size: info.total_size,
28
+ num_files: info.num_files,
29
+ last_modified: info.last_modified,
30
+ newest_file: info.newest_file,
31
+ first_modified: info.first_modified,
32
+ first_file: info.first_file
33
+ }
34
+ end
35
+
36
+ csv_headers = {
37
+ bucket: 'Bucket',
38
+ creation_date: 'Creation Date',
39
+ total_size: 'Total Size',
40
+ num_files: 'Number of Files',
41
+ last_modified: 'Last Modified',
42
+ newest_file: 'Newest File',
43
+ first_modified: 'First Modified',
44
+ first_file: 'First File'
45
+ }
46
+
47
+ file = "AWS-S3-Usage-Report-#{Time.now.strftime('%Y-%m-%dT%H%M%S')}.csv"
48
+ CSV.open(file, 'w') do |csv|
49
+ csv << csv_headers.values
50
+
51
+ bucket_info.each_value do |stats|
52
+ csv << csv_headers.keys.map { |k| stats[k] }
53
+ end
54
+ end
55
+
56
+ puts file
@@ -25,6 +25,12 @@ module S3Grep
25
25
  end
26
26
 
27
27
  def each
28
+ each_content do |content|
29
+ yield('s3://' + uri.host + '/' + escape_path(content.key))
30
+ end
31
+ end
32
+
33
+ def each_content
28
34
  uri = URI(s3_url)
29
35
 
30
36
  max_keys = 1_000
@@ -40,7 +46,7 @@ module S3Grep
40
46
  )
41
47
 
42
48
  resp.contents.each do |content|
43
- yield('s3://' + uri.host + '/' + escape_path(content.key))
49
+ yield(content)
44
50
  end
45
51
 
46
52
  while resp.contents.size == max_keys
@@ -56,7 +62,7 @@ module S3Grep
56
62
  )
57
63
 
58
64
  resp.contents.each do |content|
59
- yield('s3://' + uri.host + '/' + escape_path(content.key))
65
+ yield(content)
60
66
  end
61
67
  end
62
68
  end
@@ -64,5 +70,9 @@ module S3Grep
64
70
  def escape_path(s3_path)
65
71
  s3_path.split('/').map { |part| CGI.escape(part) }.join('/')
66
72
  end
73
+
74
+ def info
75
+ ::S3Grep::DirectoryInfo.get(self)
76
+ end
67
77
  end
68
78
  end
@@ -0,0 +1,74 @@
1
+ module S3Grep
2
+ class DirectoryInfo
3
+ attr_reader :bucket,
4
+ :base_prefix,
5
+ :total_size,
6
+ :num_files,
7
+ :newest_content,
8
+ :oldest_content,
9
+ :num_files_by_storage_class,
10
+ :total_size_by_storage_class
11
+
12
+ def self.get(directory)
13
+ info = new(directory)
14
+ info.process(directory)
15
+ end
16
+
17
+ def initialize(directory)
18
+ @total_size = 0
19
+ @num_files = 0
20
+ @num_files_by_storage_class = Hash.new(0)
21
+ @total_size_by_storage_class = Hash.new(0)
22
+ set_path(directory)
23
+ end
24
+
25
+ def process(directory)
26
+ directory.each_content do |content|
27
+ @num_files += 1
28
+ @total_size += content[:size]
29
+
30
+ @num_files_by_storage_class[content[:storage_class]] += 1
31
+ @total_size_by_storage_class[content[:storage_class]] += content[:size]
32
+
33
+ set_newest(content)
34
+ set_oldest(content)
35
+ end
36
+
37
+ self
38
+ end
39
+
40
+ def last_modified
41
+ @newest_content && @newest_content[:last_modified]
42
+ end
43
+
44
+ def newest_file
45
+ @newest_content && @newest_content[:key]
46
+ end
47
+
48
+ def first_modified
49
+ @oldest_content && @oldest_content[:last_modified]
50
+ end
51
+
52
+ def first_file
53
+ @oldest_content && @oldest_content[:key]
54
+ end
55
+
56
+ def set_path(directory)
57
+ uri = URI(directory.s3_url)
58
+ @bucket = uri.host
59
+ @base_prefix = CGI.unescape(uri.path[1..-1] || '')
60
+ end
61
+
62
+ def set_newest(content)
63
+ if @newest_content.nil? || @newest_content[:last_modified] < content[:last_modified]
64
+ @newest_content = content
65
+ end
66
+ end
67
+
68
+ def set_oldest(content)
69
+ if @oldest_content.nil? || @oldest_content[:last_modified] > content[:last_modified]
70
+ @oldest_content = content
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/s3grep.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module S3Grep
2
2
  autoload :Directory, 's3grep/directory'
3
+ autoload :DirectoryInfo, 's3grep/directory_info'
3
4
  autoload :Search, 's3grep/search'
4
5
  end
data/s3grep.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 's3grep'
5
- s.version = '0.1.5'
5
+ s.version = '0.1.9'
6
6
  s.licenses = ['MIT']
7
7
  s.summary = 'Search through S3 files'
8
8
  s.description = 'Tools for searching files on S3'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3grep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-15 00:00:00.000000000 Z
11
+ date: 2024-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-s3
@@ -29,6 +29,8 @@ email: dougyouch@gmail.com
29
29
  executables:
30
30
  - s3cat
31
31
  - s3grep
32
+ - s3info
33
+ - s3report
32
34
  extensions: []
33
35
  extra_rdoc_files: []
34
36
  files:
@@ -41,8 +43,11 @@ files:
41
43
  - README.md
42
44
  - bin/s3cat
43
45
  - bin/s3grep
46
+ - bin/s3info
47
+ - bin/s3report
44
48
  - lib/s3grep.rb
45
49
  - lib/s3grep/directory.rb
50
+ - lib/s3grep/directory_info.rb
46
51
  - lib/s3grep/search.rb
47
52
  - s3grep.gemspec
48
53
  - script/console