Skip to content

Commit 6ae5e9a

Browse files
authored
Merge pull request #12 from wallarm/dev
Some optimizations for S3 cli
2 parents 374c850 + 3a879fb commit 6ae5e9a

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

lib/cloud_storage/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module CloudStorage
4-
VERSION = '0.3.1'
4+
VERSION = '0.3.2'
55
end

lib/cloud_storage/wrappers/s3.rb

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,37 @@ def initialize(client, resource, bucket_name, **opts)
2424
@opts = opts
2525
end
2626

27-
def each
27+
def each # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
2828
return to_enum unless block_given?
2929

30-
@client.list_objects(bucket: @bucket_name, **@opts).contents.each do |item|
31-
yield Objects::S3.new \
32-
item,
33-
bucket_name: @bucket_name,
34-
resource: @resource,
35-
client: @client
30+
counter = 0
31+
limit_exceeded = false
32+
33+
@client.list_objects_v2(bucket: @bucket_name, **@opts).each do |page|
34+
page.contents.each do |item|
35+
yield build_object(item)
36+
next unless @opts[:max_keys]
37+
38+
counter += 1
39+
40+
limit_exceeded = counter >= @opts[:max_keys]
41+
break if limit_exceeded
42+
end
43+
44+
break if limit_exceeded
3645
end
3746
rescue Aws::S3::Errors::NoSuchBucket, Aws::S3::Errors::NotFound, Aws::S3::Errors::InvalidBucketName
3847
end
48+
49+
private
50+
51+
def build_object(item)
52+
Objects::S3.new \
53+
item,
54+
bucket_name: @bucket_name,
55+
resource: @resource,
56+
client: @client
57+
end
3958
end
4059

4160
def files(**opts)
@@ -101,17 +120,13 @@ def resource
101120

102121
def upload_file_or_io(key, file_or_io, **opts)
103122
if file_or_io.respond_to?(:path)
104-
transfer_manager.upload_file(file_or_io.path, bucket: @bucket_name, key: key, **opts)
123+
Aws::S3::TransferManager.new(client: client).upload_file \
124+
file_or_io.path, bucket: @bucket_name, key: key, **opts
105125
else
106-
transfer_manager.upload_stream(bucket: @bucket_name, key: key, **opts) do |write_stream|
107-
IO.copy_stream(file_or_io, write_stream)
108-
end
126+
client.put_object \
127+
bucket: @bucket_name, key: key, body: file_or_io, **opts
109128
end
110129
end
111-
112-
def transfer_manager
113-
@transfer_manager ||= Aws::S3::TransferManager.new(client: client)
114-
end
115130
end
116131
end
117132
end

0 commit comments

Comments
 (0)