Class: S3Grep::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/s3grep/directory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3_url, aws_s3_client) ⇒ Directory

Returns a new instance of Directory.



10
11
12
13
# File 'lib/s3grep/directory.rb', line 10

def initialize(s3_url, aws_s3_client)
  @s3_url = s3_url
  @aws_s3_client = aws_s3_client
end

Instance Attribute Details

#aws_s3_clientObject (readonly)

Returns the value of attribute aws_s3_client.



7
8
9
# File 'lib/s3grep/directory.rb', line 7

def aws_s3_client
  @aws_s3_client
end

#s3_urlObject (readonly)

Returns the value of attribute s3_url.



7
8
9
# File 'lib/s3grep/directory.rb', line 7

def s3_url
  @s3_url
end

Class Method Details

.glob(s3_url, aws_s3_client, regex, &block) ⇒ Object



15
16
17
# File 'lib/s3grep/directory.rb', line 15

def self.glob(s3_url, aws_s3_client, regex, &block)
  new(s3_url, aws_s3_client).glob(regex, &block)
end

Instance Method Details

#eachObject



27
28
29
30
31
# File 'lib/s3grep/directory.rb', line 27

def each
  each_content do |content|
    yield('s3://' + uri.host + '/' + escape_path(content.key))
  end
end

#each_contentObject



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
# File 'lib/s3grep/directory.rb', line 33

def each_content
  uri = URI(s3_url)

  max_keys = 1_000

  prefix = CGI.unescape(uri.path[1..-1] || '')

  resp = aws_s3_client.list_objects(
    {
      bucket: uri.host,
      prefix: prefix,
      max_keys: max_keys
    }
  )

  resp.contents.each do |content|
    yield(content)
  end

  while resp.contents.size == max_keys
    marker = resp.contents.last.key

    resp = aws_s3_client.list_objects(
      {
        bucket: uri.host,
        prefix: prefix,
        max_keys: max_keys,
        marker: marker
      }
    )

    resp.contents.each do |content|
      yield(content)
    end
  end
end

#escape_path(s3_path) ⇒ Object



70
71
72
# File 'lib/s3grep/directory.rb', line 70

def escape_path(s3_path)
  s3_path.split('/').map { |part| CGI.escape(part) }.join('/')
end

#glob(regex) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/s3grep/directory.rb', line 19

def glob(regex)
  each do |s3_file|
    next unless s3_file.match?(regex)

    yield s3_file
  end
end

#infoObject



74
75
76
# File 'lib/s3grep/directory.rb', line 74

def info
  ::S3Grep::DirectoryInfo.get(self)
end