Class: S3Light::Client::ObjectsList

Inherits:
Object
  • Object
show all
Defined in:
lib/s3-light/client/objects_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, bucket) ⇒ ObjectsList

Returns a new instance of ObjectsList.



6
7
8
9
# File 'lib/s3-light/client/objects_list.rb', line 6

def initialize(client, bucket)
  @client = client
  @bucket = bucket
end

Instance Method Details

#allObject



11
12
13
14
15
16
17
18
19
# File 'lib/s3-light/client/objects_list.rb', line 11

def all
  response = @client.with_connection do |connection|
    connection.make_request(:get, "/#{@bucket.name}")
  end

  response.xml.remove_namespaces!.xpath('//Contents').map do |object|
    S3Light::Object.new(@client, @bucket, object.xpath('Key').text, nil, true)
  end
end

#create_batch(input:, concurrency: 10) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/s3-light/client/objects_list.rb', line 50

def create_batch(input:, concurrency: 10)
  result = ConcurrentResult.new

  thread_poll = @client.build_thread_poll(concurrency)
  current_thread = Thread.current

  input.each do |key, input|
    thread_poll.with_connection do |connection|
      object = S3Light::Object.new(@client, @bucket, key, input, true)

      object.__save!(connection)

      result.add(key, object)
    rescue Exception => e
      thread_poll.kill
      current_thread.raise(e)
    end
  end

  thread_poll.wait_to_finish

  result.to_h
ensure
  thread_poll.close
end

#destroy_batch(keys:, concurrency: 10) ⇒ Object



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
102
103
104
105
106
107
# File 'lib/s3-light/client/objects_list.rb', line 76

def destroy_batch(keys:, concurrency: 10)
  result = ConcurrentResult.new

  thread_poll = @client.build_thread_poll(concurrency)
  current_thread = Thread.current

  keys.each do |key|
    thread_poll.with_connection do |connection|
      object = S3Light::Object.new(@client, @bucket, key, nil, true)

      object.__destroy!(connection)

      result.add(key, true)
    rescue S3Light::Connection::HttpError => e
      if e.code == 404
        result.add(key, true)
      else
        thread_poll.kill
        current_thread.raise(e)
      end
    rescue Exception => e
      thread_poll.kill
      current_thread.raise(e)
    end
  end

  thread_poll.wait_to_finish

  result.to_h
ensure
  thread_poll.close
end

#download_batch(keys:, to: '/tmp', concurrency: 10) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/s3-light/client/objects_list.rb', line 170

def download_batch(keys:, to: '/tmp', concurrency: 10)
  raise S3Light::Error, 'Invalid download path' unless File.directory?(to)
  to_path = Pathname.new(to)

  result = ConcurrentResult.new

  thread_poll = @client.build_thread_poll(concurrency)
  current_thread = Thread.current

  keys.each do |key|
    thread_poll.with_connection do |connection|
      download_path = to_path.join("#{SecureRandom.hex}-#{key}").to_s
      connection.download_file("/#{@bucket.name}/#{key}", download_path)

      result.add(key, download_path)
    rescue S3Light::Connection::HttpError => e
      if e.code == 404
        result.add(key, nil)
      else
        thread_poll.kill
        current_thread.raise(e)
      end
    rescue Exception => e
      thread_poll.kill
      current_thread.raise(e)
    end
  end

  thread_poll.wait_to_finish

  result.to_h
ensure
  thread_poll&.close
end

#exists?(key:) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/s3-light/client/objects_list.rb', line 34

def exists?(key:)
  @client.with_connection do |connection|
    connection.make_request(:head, "/#{@bucket.name}/#{key}")
  end

  true
rescue S3Light::Connection::HttpError => e
  return false if e.code == 404

  raise e
end

#exists_batch?(keys:, concurrency: 10) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/s3-light/client/objects_list.rb', line 109

def exists_batch?(keys:, concurrency: 10)
  result = ConcurrentResult.new

  thread_poll = @client.build_thread_poll(concurrency)
  current_thread = Thread.current

  keys.each do |key|
    thread_poll.with_connection do |connection|
      result.add(key, connection.make_request(:head, "/#{@bucket.name}/#{key}"))

    rescue S3Light::Connection::HttpError => e
      if e.code == 404
        result.add(key, false)
      else
        thread_poll.kill
        current_thread.raise(e)
      end
    rescue Exception => e
      thread_poll.kill
      current_thread.raise(e)
    end
  end

  thread_poll.wait_to_finish

  result.to_h
ensure
  thread_poll.close
end

#find_by(key:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/s3-light/client/objects_list.rb', line 21

def find_by(key:)
  @client.with_connection do |connection|
    connection.make_request(:head, "/#{@bucket.name}/#{key}")
  end

  S3Light::Object.new(@client, @bucket, key, nil, true)

rescue S3Light::Connection::HttpError => e
  return nil if e.code == 404

  raise e
end

#find_by_batch(keys:, concurrency: 10) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/s3-light/client/objects_list.rb', line 139

def find_by_batch(keys:, concurrency: 10)
  result = ConcurrentResult.new

  thread_poll = @client.build_thread_poll(concurrency)
  current_thread = Thread.current

  keys.each do |key|
    thread_poll.with_connection do |connection|
      connection.make_request(:head, "/#{@bucket.name}/#{key}")

      result.add(key, S3Light::Object.new(@client, @bucket, key, nil, true))
    rescue S3Light::Connection::HttpError => e
      if e.code == 404
        result.add(key, nil)
      else
        thread_poll.kill
        current_thread.raise(e)
      end
    rescue Exception => e
      thread_poll.kill
      current_thread.raise(e)
    end
  end

  thread_poll.wait_to_finish

  result.to_h
ensure
  thread_poll.close
end

#new(key:, input: nil) ⇒ Object



46
47
48
# File 'lib/s3-light/client/objects_list.rb', line 46

def new(key:, input: nil)
  S3Light::Object.new(@client, @bucket, key, input, false)
end