s3grep 0.1.5 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 516c9439c60a587b2c9d20b2bc5b9203036f4df390a0c5f23e4920516669ff42
4
- data.tar.gz: ee817321b9d23fdb6d4af1b31cf31ec4f0fb836dfde3216ac63b1225ad85810f
3
+ metadata.gz: 804681319bd1ddfd4ff89ab54f21f9cff854b06d3a254ed7870da7be8dcfb059
4
+ data.tar.gz: f615b79c46f460a73f530fbe075e16f9c9ee9dd4c778cf26eee9ad63eea15ae7
5
5
  SHA512:
6
- metadata.gz: f0373f0c049d7fa1a07279cb727b4d7e8950d8da0d8fe7de83189b633152acf66c150feb4c16b59c9ed7c17bab38b135a6a9a83700c05b1e24a0b53aaf7824c4
7
- data.tar.gz: 3ec8bef5e1c31339d26bd0f45bd213f2cb9e2fd08f8372a20bbca391f8167af70081a90d6e613c315b0e175ff0c4af67c4c59edf9270f6b381787fa2829f8bb3
6
+ metadata.gz: 6ac540deed47a2e02ab396b0f92c018ae1ec608b4824b4adb13e2d9e5fe4631366879fdfb176cd2dfe7bbee7ec040ce53d74a95f92e30994d1b1e87120d09e6a
7
+ data.tar.gz: 8eaa9a8e01415551c0e0770ac59b4cf8e5374f75317fbf2a9ee60f3bd16766b072acd4b0d440a79af6013a2dd221b06c7ced8a1ffbcecd8973e95397225f6695
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,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 's3grep'
5
+ require 'aws-sdk-s3'
6
+ require 'json'
7
+
8
+ s3_file = ARGV[0]
9
+ aws_s3_client = Aws::S3::Client.new
10
+ info = S3Grep::Directory.new(s3_file, aws_s3_client).info
11
+
12
+ stats = {
13
+ bucket: info.bucket,
14
+ base_prefix: info.base_prefix,
15
+ total_size: info.total_size,
16
+ num_files: info.num_files,
17
+ last_modified: info.last_modified,
18
+ newest_file: info.newest_file,
19
+ first_modified: info.first_modified,
20
+ first_file: info.first_file
21
+ }
22
+
23
+ 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,67 @@
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
+
10
+ def self.get(directory)
11
+ info = new(directory)
12
+ info.process(directory)
13
+ end
14
+
15
+ def initialize(directory)
16
+ @total_size = 0
17
+ @num_files = 0
18
+ set_path(directory)
19
+ end
20
+
21
+ def process(directory)
22
+ directory.each_content do |content|
23
+ @num_files += 1
24
+ @total_size += content[:size]
25
+
26
+ set_newest(content)
27
+ set_oldest(content)
28
+ end
29
+
30
+ self
31
+ end
32
+
33
+ def last_modified
34
+ @newest_content && @newest_content[:last_modified]
35
+ end
36
+
37
+ def newest_file
38
+ @newest_content && @newest_content[:key]
39
+ end
40
+
41
+ def first_modified
42
+ @oldest_content && @oldest_content[:last_modified]
43
+ end
44
+
45
+ def first_file
46
+ @oldest_content && @oldest_content[:key]
47
+ end
48
+
49
+ def set_path(directory)
50
+ uri = URI(directory.s3_url)
51
+ @bucket = uri.host
52
+ @base_prefix = CGI.unescape(uri.path[1..-1] || '')
53
+ end
54
+
55
+ def set_newest(content)
56
+ if @newest_content.nil? || @newest_content[:last_modified] < content[:last_modified]
57
+ @newest_content = content
58
+ end
59
+ end
60
+
61
+ def set_oldest(content)
62
+ if @oldest_content.nil? || @oldest_content[:last_modified] > content[:last_modified]
63
+ @oldest_content = content
64
+ end
65
+ end
66
+ end
67
+ 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.8'
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.8
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-08-23 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