s3-light 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +184 -0
- data/Rakefile +8 -0
- data/lefthook.yml +7 -0
- data/lib/s3-light/bucket.rb +61 -0
- data/lib/s3-light/client/buckets_list.rb +118 -0
- data/lib/s3-light/client/objects_list.rb +206 -0
- data/lib/s3-light/client.rb +54 -0
- data/lib/s3-light/concurrent_result.rb +21 -0
- data/lib/s3-light/configuration.rb +11 -0
- data/lib/s3-light/connection/body.rb +64 -0
- data/lib/s3-light/connection/response.rb +25 -0
- data/lib/s3-light/connection.rb +191 -0
- data/lib/s3-light/connections_manager/main.rb +43 -0
- data/lib/s3-light/connections_manager/threaded.rb +44 -0
- data/lib/s3-light/md5_calculator.rb +49 -0
- data/lib/s3-light/object.rb +133 -0
- data/lib/s3-light/version.rb +5 -0
- data/lib/s3-light.rb +28 -0
- data/sig/s3/light.rbs +6 -0
- data/tmp/.keep +0 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9c5fee12967ac56da5d7bbe3311bf53f3ba296090663048797f2459f1dea048c
|
4
|
+
data.tar.gz: b8693107cd81c5a9df4c55ea96fb1080fd1ab9d21320b8d12179e581051b19f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 984f369925150fe8f7bf3a0a0327af7f81587699c5ca3b7bb7f348f3bea585b4b1da9209fb6d30ab8879cbc113363e66664eeeb46e74a7f992b9b4936bd845c8
|
7
|
+
data.tar.gz: 79f1f21475df7c1d86ee537969c486baca5528e81f873b3a38289249b833e4d330085e9f59f3d6594b7f737017e9ee2fd9fa297d4b795a0bc9c34d1a88d50601
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 sebi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
# S3::Light ๐
|
2
|
+
|
3
|
+
S3::Light is a fast, lightweight Ruby gem for interacting with S3-compatible storage services. It's designed to be simple, efficient, and powerful! ๐ช
|
4
|
+
|
5
|
+
## Features ๐
|
6
|
+
|
7
|
+
- ๐ Lightning-fast performance
|
8
|
+
- ๐ชถ Lightweight design
|
9
|
+
- ๐ง Easy to use and integrate
|
10
|
+
- ๐ฆ Batch operations support
|
11
|
+
- ๐ Concurrent processing
|
12
|
+
- ๐ Compatible with S3 and S3-like services (e.g., MinIO)
|
13
|
+
- ๐ Connection reuse and keep-alive
|
14
|
+
- ๐งต Thread-safe connection management
|
15
|
+
|
16
|
+
## Installation ๐ฅ
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 's3-light'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle install
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install s3-light
|
31
|
+
|
32
|
+
## Usage ๐ ๏ธ
|
33
|
+
|
34
|
+
### Basic Operations
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# Initialize the client
|
38
|
+
client = S3Light::Client.new(
|
39
|
+
access_key_id: 'your_access_key',
|
40
|
+
secret_access_key: 'your_secret_key',
|
41
|
+
region: 'us-east-1'
|
42
|
+
)
|
43
|
+
|
44
|
+
# List all buckets
|
45
|
+
buckets = client.buckets.all
|
46
|
+
|
47
|
+
# Create a new bucket
|
48
|
+
new_bucket = client.buckets.new(name: 'my-awesome-bucket').save!
|
49
|
+
|
50
|
+
# Upload an object
|
51
|
+
object = new_bucket.objects.new(key: 'hello.txt', input: 'Hello, World!').save!
|
52
|
+
|
53
|
+
# Download an object
|
54
|
+
downloaded_content = object.download(to: '/tmp/hello.txt')
|
55
|
+
|
56
|
+
# Delete an object
|
57
|
+
object.destroy!
|
58
|
+
|
59
|
+
# Delete a bucket
|
60
|
+
new_bucket.destroy!
|
61
|
+
```
|
62
|
+
|
63
|
+
### Batch Operations ๐๏ธ
|
64
|
+
|
65
|
+
S3::Light shines when it comes to batch operations! Here are some examples:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# Create multiple buckets at once
|
69
|
+
new_buckets = client.buckets.create_batch(names: ['bucket1', 'bucket2', 'bucket3'])
|
70
|
+
|
71
|
+
# Check if multiple buckets exist
|
72
|
+
existence = client.buckets.exists_batch?(names: ['bucket1', 'bucket2', 'nonexistent-bucket'])
|
73
|
+
|
74
|
+
# Upload multiple objects concurrently
|
75
|
+
bucket = client.buckets.find_by(name: 'my-bucket')
|
76
|
+
objects = bucket.objects.create_batch(
|
77
|
+
input: {
|
78
|
+
'file1.txt' => 'Content 1',
|
79
|
+
'file2.txt' => 'Content 2',
|
80
|
+
'file3.txt' => File.open('path/to/file3.txt')
|
81
|
+
},
|
82
|
+
concurrency: 5
|
83
|
+
)
|
84
|
+
|
85
|
+
# Download multiple objects concurrently
|
86
|
+
downloads = bucket.objects.download_batch(
|
87
|
+
keys: ['file1.txt', 'file2.txt', 'file3.txt'],
|
88
|
+
to: '/tmp',
|
89
|
+
concurrency: 5
|
90
|
+
)
|
91
|
+
```
|
92
|
+
|
93
|
+
## Performance ๐๏ธ
|
94
|
+
|
95
|
+
S3::Light is designed to be fast and efficient. Here's how it achieves its high performance:
|
96
|
+
|
97
|
+
# S3::Light ๐
|
98
|
+
|
99
|
+
... [previous content remains the same] ...
|
100
|
+
|
101
|
+
## Performance Benchmarks ๐
|
102
|
+
|
103
|
+
We've conducted benchmarks comparing S3::Light with the Amazon S3 gem. The benchmark tests include uploading and downloading a 50MB file 50 times individually.
|
104
|
+
|
105
|
+
Here's a summary of the benchmark results:
|
106
|
+
|
107
|
+
| Operation | S3::Light | Amazon S3 Gem | Performance Difference |
|
108
|
+
|-------------------|-----------|---------------|------------------------|
|
109
|
+
| Individual Upload | 22.56s | 37.98s | 41% faster |
|
110
|
+
| Individual Download | 27.24s | 28.38s | 4% faster |
|
111
|
+
|
112
|
+
These results demonstrate that S3::Light offers significant performance improvements for individual upload operations and is slightly faster for individual downloads.
|
113
|
+
|
114
|
+
S3::Light shows its strength in:
|
115
|
+
- Individual file uploads (41% faster)
|
116
|
+
- Individual file downloads (4% faster)
|
117
|
+
|
118
|
+
It's important to note that performance can vary depending on factors such as network conditions, server load, and specific use cases. We recommend running benchmarks in your own environment to get the most accurate results for your specific use case.
|
119
|
+
|
120
|
+
### Key Observations:
|
121
|
+
|
122
|
+
1. **Individual Uploads**: S3::Light significantly outperforms the Amazon S3 gem, completing the task in about 59% of the time taken by the Amazon S3 gem.
|
123
|
+
|
124
|
+
2. **Individual Downloads**: S3::Light is slightly faster than the Amazon S3 gem, with a small but noticeable performance advantage.
|
125
|
+
|
126
|
+
These results highlight S3::Light's efficiency in handling individual file operations, particularly uploads, which could be beneficial for applications that frequently deal with single-file transfers.
|
127
|
+
|
128
|
+
You can find the full benchmark script in the `benchmark` directory of this repository. Feel free to run it yourself and compare the results in your specific environment.
|
129
|
+
|
130
|
+
... [rest of the content remains the same] ...
|
131
|
+
|
132
|
+
### Connection Reuse and Keep-Alive ๐
|
133
|
+
|
134
|
+
S3::Light implements connection reuse and keep-alive strategies to minimize the overhead of establishing new connections for each request. This significantly reduces latency, especially for applications that make frequent API calls.
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
# The client automatically reuses connections
|
138
|
+
client.buckets.all # First request establishes a connection
|
139
|
+
client.buckets.all # Subsequent requests reuse the same connection
|
140
|
+
```
|
141
|
+
|
142
|
+
### Connection Pooling ๐
|
143
|
+
|
144
|
+
For concurrent operations, S3::Light uses a connection pool. This allows multiple threads to work simultaneously without the overhead of creating new connections for each thread.
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
# Connection pooling is automatically used in batch operations
|
148
|
+
bucket.objects.download_batch(keys: large_list_of_keys, concurrency: 10)
|
149
|
+
```
|
150
|
+
|
151
|
+
### Thread-Safe Connection Management ๐งต
|
152
|
+
|
153
|
+
S3::Light ensures thread-safe connection management, allowing you to use it safely in multi-threaded environments without worrying about race conditions or data corruption.
|
154
|
+
|
155
|
+
### Efficient Batch Processing ๐ฆ
|
156
|
+
|
157
|
+
Batch operations in S3::Light are designed to maximize throughput by processing multiple items concurrently while efficiently managing resources.
|
158
|
+
|
159
|
+
## Best Practices ๐
|
160
|
+
|
161
|
+
To get the most out of S3::Light's performance features:
|
162
|
+
|
163
|
+
1. Reuse client instances when possible to take advantage of connection reuse.
|
164
|
+
2. Use batch operations for handling multiple items instead of processing them one by one.
|
165
|
+
3. Experiment with the `concurrency` parameter in batch operations to find the optimal setting for your use case and infrastructure.
|
166
|
+
4. Remember to call `S3Light::Client.close_all_connections` when your application is shutting down to properly release all resources.
|
167
|
+
|
168
|
+
## Development ๐ ๏ธ
|
169
|
+
|
170
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
171
|
+
|
172
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
173
|
+
|
174
|
+
## Contributing ๐ค
|
175
|
+
|
176
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sebyx07/s3-light. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/sebyx07/s3-light/blob/master/CODE_OF_CONDUCT.md).
|
177
|
+
|
178
|
+
## License ๐
|
179
|
+
|
180
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
181
|
+
|
182
|
+
## Code of Conduct ๐ค
|
183
|
+
|
184
|
+
Everyone interacting in the S3::Light project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sebyx07/s3-light/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/lefthook.yml
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module S3Light
|
4
|
+
Bucket = Struct.new(:client, :name, :persisted) do
|
5
|
+
def initialize(client, name, persisted = false)
|
6
|
+
super(client, name, persisted)
|
7
|
+
end
|
8
|
+
|
9
|
+
def save!
|
10
|
+
if persisted
|
11
|
+
raise S3Light::Error, 'Bucket already exists'
|
12
|
+
end
|
13
|
+
|
14
|
+
if name.nil? || name.empty?
|
15
|
+
raise S3Light::Error, 'Bucket name is required'
|
16
|
+
end
|
17
|
+
|
18
|
+
self.client.with_connection do |connection|
|
19
|
+
__save!(connection)
|
20
|
+
end
|
21
|
+
|
22
|
+
self.persisted = true
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def destroy!
|
27
|
+
unless persisted
|
28
|
+
raise S3Light::Error, 'Bucket does not exist'
|
29
|
+
end
|
30
|
+
|
31
|
+
self.client.with_connection do |connection|
|
32
|
+
__destroy!(connection)
|
33
|
+
rescue S3Light::Connection::HttpError => e
|
34
|
+
raise e unless e.code == 404
|
35
|
+
end
|
36
|
+
|
37
|
+
self.persisted = false
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
def objects
|
42
|
+
@objects ||= S3Light::Client::ObjectsList.new(client, self)
|
43
|
+
end
|
44
|
+
|
45
|
+
def __destroy!(connection)
|
46
|
+
connection.make_request(:delete, "/#{name}")
|
47
|
+
end
|
48
|
+
|
49
|
+
def __save!(connection)
|
50
|
+
connection.make_request(:put, "/#{name}")
|
51
|
+
end
|
52
|
+
|
53
|
+
def persisted?
|
54
|
+
persisted
|
55
|
+
end
|
56
|
+
|
57
|
+
def inspect
|
58
|
+
"#<#{self.class.name} @name=#{name} @persisted=#{persisted}>"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module S3Light
|
4
|
+
class Client
|
5
|
+
class BucketsList
|
6
|
+
def initialize(client)
|
7
|
+
@client = client
|
8
|
+
end
|
9
|
+
|
10
|
+
def all
|
11
|
+
response = @client.with_connection do |connection|
|
12
|
+
connection.make_request(:get, '/')
|
13
|
+
end
|
14
|
+
|
15
|
+
response.xml.remove_namespaces!.xpath('//Bucket').map do |bucket|
|
16
|
+
S3Light::Bucket.new(@client, bucket.xpath('Name').text, true)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def exists?(name:)
|
21
|
+
response = @client.with_connection do |connection|
|
22
|
+
connection.make_request(:head, "/#{name}")
|
23
|
+
end
|
24
|
+
|
25
|
+
response.code == 200
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_batch(names:, concurrency: 10)
|
29
|
+
result = ConcurrentResult.new
|
30
|
+
|
31
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
32
|
+
current_thread = Thread.current
|
33
|
+
|
34
|
+
names.each do |name|
|
35
|
+
thread_poll.with_connection do |connection|
|
36
|
+
bucket = S3Light::Bucket.new(@client, name, true)
|
37
|
+
bucket.__save!(connection)
|
38
|
+
result.add(name, bucket)
|
39
|
+
rescue Exception => e
|
40
|
+
thread_poll.kill
|
41
|
+
current_thread.raise(e)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
thread_poll.wait_to_finish
|
46
|
+
|
47
|
+
result.to_h
|
48
|
+
ensure
|
49
|
+
thread_poll.close
|
50
|
+
end
|
51
|
+
|
52
|
+
def destroy_batch(names:, concurrency: 10)
|
53
|
+
result = ConcurrentResult.new
|
54
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
55
|
+
current_thread = Thread.current
|
56
|
+
|
57
|
+
names.each do |name|
|
58
|
+
thread_poll.with_connection do |connection|
|
59
|
+
new(name: name).__destroy!(connection)
|
60
|
+
result.add(name, true)
|
61
|
+
|
62
|
+
rescue Exception => e
|
63
|
+
thread_poll.kill
|
64
|
+
current_thread.raise(e)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
thread_poll.wait_to_finish
|
69
|
+
|
70
|
+
result.to_h
|
71
|
+
ensure
|
72
|
+
thread_poll.close
|
73
|
+
end
|
74
|
+
|
75
|
+
def exists_batch?(names:, concurrency: 10)
|
76
|
+
result = ConcurrentResult.new
|
77
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
78
|
+
current_thread = Thread.current
|
79
|
+
|
80
|
+
names.each do |name|
|
81
|
+
thread_poll.with_connection do |connection|
|
82
|
+
result.add(name, connection.make_request(:head, "/#{name}"))
|
83
|
+
end
|
84
|
+
rescue Exception => e
|
85
|
+
thread_poll.kill
|
86
|
+
current_thread.raise(e)
|
87
|
+
end
|
88
|
+
|
89
|
+
thread_poll.wait_to_finish
|
90
|
+
|
91
|
+
result.to_h
|
92
|
+
ensure
|
93
|
+
thread_poll.close
|
94
|
+
end
|
95
|
+
|
96
|
+
def find_by(name:)
|
97
|
+
@client.with_connection do |connection|
|
98
|
+
connection.make_request(:get, "/#{name}")
|
99
|
+
end
|
100
|
+
|
101
|
+
S3Light::Bucket.new(@client, name, true)
|
102
|
+
|
103
|
+
rescue S3Light::Connection::HttpError => e
|
104
|
+
return nil if e.code == 404
|
105
|
+
|
106
|
+
raise e
|
107
|
+
end
|
108
|
+
|
109
|
+
def new(name: nil)
|
110
|
+
S3Light::Bucket.new(@client, name, false)
|
111
|
+
end
|
112
|
+
|
113
|
+
def inspect
|
114
|
+
"#<#{self.class.name} @buckets=#{all.size}>"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module S3Light
|
4
|
+
class Client
|
5
|
+
class ObjectsList
|
6
|
+
def initialize(client, bucket)
|
7
|
+
@client = client
|
8
|
+
@bucket = bucket
|
9
|
+
end
|
10
|
+
|
11
|
+
def all
|
12
|
+
response = @client.with_connection do |connection|
|
13
|
+
connection.make_request(:get, "/#{@bucket.name}")
|
14
|
+
end
|
15
|
+
|
16
|
+
response.xml.remove_namespaces!.xpath('//Contents').map do |object|
|
17
|
+
S3Light::Object.new(@client, @bucket, object.xpath('Key').text, nil, true)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_by(key:)
|
22
|
+
@client.with_connection do |connection|
|
23
|
+
connection.make_request(:head, "/#{@bucket.name}/#{key}")
|
24
|
+
end
|
25
|
+
|
26
|
+
S3Light::Object.new(@client, @bucket, key, nil, true)
|
27
|
+
|
28
|
+
rescue S3Light::Connection::HttpError => e
|
29
|
+
return nil if e.code == 404
|
30
|
+
|
31
|
+
raise e
|
32
|
+
end
|
33
|
+
|
34
|
+
def exists?(key:)
|
35
|
+
@client.with_connection do |connection|
|
36
|
+
connection.make_request(:head, "/#{@bucket.name}/#{key}")
|
37
|
+
end
|
38
|
+
|
39
|
+
true
|
40
|
+
rescue S3Light::Connection::HttpError => e
|
41
|
+
return false if e.code == 404
|
42
|
+
|
43
|
+
raise e
|
44
|
+
end
|
45
|
+
|
46
|
+
def new(key:, input: nil)
|
47
|
+
S3Light::Object.new(@client, @bucket, key, input, false)
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_batch(input:, concurrency: 10)
|
51
|
+
result = ConcurrentResult.new
|
52
|
+
|
53
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
54
|
+
current_thread = Thread.current
|
55
|
+
|
56
|
+
input.each do |key, input|
|
57
|
+
thread_poll.with_connection do |connection|
|
58
|
+
object = S3Light::Object.new(@client, @bucket, key, input, true)
|
59
|
+
|
60
|
+
object.__save!(connection)
|
61
|
+
|
62
|
+
result.add(key, object)
|
63
|
+
rescue Exception => e
|
64
|
+
thread_poll.kill
|
65
|
+
current_thread.raise(e)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
thread_poll.wait_to_finish
|
70
|
+
|
71
|
+
result.to_h
|
72
|
+
ensure
|
73
|
+
thread_poll.close
|
74
|
+
end
|
75
|
+
|
76
|
+
def destroy_batch(keys:, concurrency: 10)
|
77
|
+
result = ConcurrentResult.new
|
78
|
+
|
79
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
80
|
+
current_thread = Thread.current
|
81
|
+
|
82
|
+
keys.each do |key|
|
83
|
+
thread_poll.with_connection do |connection|
|
84
|
+
object = S3Light::Object.new(@client, @bucket, key, nil, true)
|
85
|
+
|
86
|
+
object.__destroy!(connection)
|
87
|
+
|
88
|
+
result.add(key, true)
|
89
|
+
rescue S3Light::Connection::HttpError => e
|
90
|
+
if e.code == 404
|
91
|
+
result.add(key, true)
|
92
|
+
else
|
93
|
+
thread_poll.kill
|
94
|
+
current_thread.raise(e)
|
95
|
+
end
|
96
|
+
rescue Exception => e
|
97
|
+
thread_poll.kill
|
98
|
+
current_thread.raise(e)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
thread_poll.wait_to_finish
|
103
|
+
|
104
|
+
result.to_h
|
105
|
+
ensure
|
106
|
+
thread_poll.close
|
107
|
+
end
|
108
|
+
|
109
|
+
def exists_batch?(keys:, concurrency: 10)
|
110
|
+
result = ConcurrentResult.new
|
111
|
+
|
112
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
113
|
+
current_thread = Thread.current
|
114
|
+
|
115
|
+
keys.each do |key|
|
116
|
+
thread_poll.with_connection do |connection|
|
117
|
+
result.add(key, connection.make_request(:head, "/#{@bucket.name}/#{key}"))
|
118
|
+
|
119
|
+
rescue S3Light::Connection::HttpError => e
|
120
|
+
if e.code == 404
|
121
|
+
result.add(key, false)
|
122
|
+
else
|
123
|
+
thread_poll.kill
|
124
|
+
current_thread.raise(e)
|
125
|
+
end
|
126
|
+
rescue Exception => e
|
127
|
+
thread_poll.kill
|
128
|
+
current_thread.raise(e)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
thread_poll.wait_to_finish
|
133
|
+
|
134
|
+
result.to_h
|
135
|
+
ensure
|
136
|
+
thread_poll.close
|
137
|
+
end
|
138
|
+
|
139
|
+
def find_by_batch(keys:, concurrency: 10)
|
140
|
+
result = ConcurrentResult.new
|
141
|
+
|
142
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
143
|
+
current_thread = Thread.current
|
144
|
+
|
145
|
+
keys.each do |key|
|
146
|
+
thread_poll.with_connection do |connection|
|
147
|
+
connection.make_request(:head, "/#{@bucket.name}/#{key}")
|
148
|
+
|
149
|
+
result.add(key, S3Light::Object.new(@client, @bucket, key, nil, true))
|
150
|
+
rescue S3Light::Connection::HttpError => e
|
151
|
+
if e.code == 404
|
152
|
+
result.add(key, nil)
|
153
|
+
else
|
154
|
+
thread_poll.kill
|
155
|
+
current_thread.raise(e)
|
156
|
+
end
|
157
|
+
rescue Exception => e
|
158
|
+
thread_poll.kill
|
159
|
+
current_thread.raise(e)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
thread_poll.wait_to_finish
|
164
|
+
|
165
|
+
result.to_h
|
166
|
+
ensure
|
167
|
+
thread_poll.close
|
168
|
+
end
|
169
|
+
|
170
|
+
def download_batch(keys:, to: '/tmp', concurrency: 10)
|
171
|
+
raise S3Light::Error, 'Invalid download path' unless File.directory?(to)
|
172
|
+
to_path = Pathname.new(to)
|
173
|
+
|
174
|
+
result = ConcurrentResult.new
|
175
|
+
|
176
|
+
thread_poll = @client.build_thread_poll(concurrency)
|
177
|
+
current_thread = Thread.current
|
178
|
+
|
179
|
+
keys.each do |key|
|
180
|
+
thread_poll.with_connection do |connection|
|
181
|
+
download_path = to_path.join("#{SecureRandom.hex}-#{key}").to_s
|
182
|
+
connection.download_file("/#{@bucket.name}/#{key}", download_path)
|
183
|
+
|
184
|
+
result.add(key, download_path)
|
185
|
+
rescue S3Light::Connection::HttpError => e
|
186
|
+
if e.code == 404
|
187
|
+
result.add(key, nil)
|
188
|
+
else
|
189
|
+
thread_poll.kill
|
190
|
+
current_thread.raise(e)
|
191
|
+
end
|
192
|
+
rescue Exception => e
|
193
|
+
thread_poll.kill
|
194
|
+
current_thread.raise(e)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
thread_poll.wait_to_finish
|
199
|
+
|
200
|
+
result.to_h
|
201
|
+
ensure
|
202
|
+
thread_poll&.close
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module S3Light
|
4
|
+
class Client
|
5
|
+
attr_reader :access_key_id, :secret_access_key, :region, :ssl_context, :endpoint
|
6
|
+
def initialize(endpoint: nil, access_key_id:, secret_access_key:, region: 'us-east-1', ssl_context: nil)
|
7
|
+
@access_key_id = access_key_id
|
8
|
+
@secret_access_key = secret_access_key
|
9
|
+
@region = region
|
10
|
+
@ssl_context = ssl_context
|
11
|
+
@endpoint = endpoint || amazon_default_endpoint
|
12
|
+
end
|
13
|
+
|
14
|
+
def buckets
|
15
|
+
@buckets ||= BucketsList.new(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def connection
|
19
|
+
return @connection if @connection
|
20
|
+
|
21
|
+
@connection = Connection.new(
|
22
|
+
endpoint: @endpoint, access_key_id: @access_key_id, secret_access_key: @secret_access_key,
|
23
|
+
ssl_context: @ssl_context
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def with_connection(&block)
|
28
|
+
self.class.main_connections_manager.yield_connection(self, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_thread_poll(concurrency)
|
32
|
+
S3Light::ConnectionsManager::Threaded.new(self, concurrency)
|
33
|
+
end
|
34
|
+
|
35
|
+
class << self
|
36
|
+
def main_connections_manager
|
37
|
+
@main_connections_manager ||= ConnectionsManager::Main.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def close_all_connections
|
41
|
+
main_connections_manager.close_all_connections
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
def amazon_default_endpoint
|
47
|
+
"https://s3.#{@region}.amazonaws.com"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
at_exit do
|
53
|
+
S3Light::Client.close_all_connections
|
54
|
+
end
|