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 +4 -4
- data/.gitignore +2 -0
- data/bin/s3info +23 -0
- data/bin/s3report +56 -0
- data/lib/s3grep/directory.rb +12 -2
- data/lib/s3grep/directory_info.rb +67 -0
- data/lib/s3grep.rb +1 -0
- data/s3grep.gemspec +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 804681319bd1ddfd4ff89ab54f21f9cff854b06d3a254ed7870da7be8dcfb059
|
4
|
+
data.tar.gz: f615b79c46f460a73f530fbe075e16f9c9ee9dd4c778cf26eee9ad63eea15ae7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ac540deed47a2e02ab396b0f92c018ae1ec608b4824b4adb13e2d9e5fe4631366879fdfb176cd2dfe7bbee7ec040ce53d74a95f92e30994d1b1e87120d09e6a
|
7
|
+
data.tar.gz: 8eaa9a8e01415551c0e0770ac59b4cf8e5374f75317fbf2a9ee60f3bd16766b072acd4b0d440a79af6013a2dd221b06c7ced8a1ffbcecd8973e95397225f6695
|
data/.gitignore
CHANGED
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
|
data/lib/s3grep/directory.rb
CHANGED
@@ -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(
|
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(
|
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
data/s3grep.gemspec
CHANGED
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.
|
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:
|
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
|