Skip to content

Commit c12d316

Browse files
Optional configuration to use an existing Awscr::S3::Client (#8)
fixes #5
1 parent 2ee2069 commit c12d316

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

spec/s3/client_spec.cr

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ def with_client(*, auto_close = true, & : EasyAwscr::S3::Client -> Nil)
6666
end
6767
end
6868

69+
def with_native_awscr_s3_client(& : Awscr::S3::Client -> Nil)
70+
with_aws_api do |api|
71+
cred = api.credential_provider.credentials
72+
yield Awscr::S3::Client.new(
73+
api.region,
74+
cred.access_key_id,
75+
cred.secret_access_key,
76+
cred.session_token,
77+
endpoint: api.endpoint
78+
)
79+
end
80+
end
81+
6982
class SafeKeyGen
7083
getter keys_used
7184

@@ -283,4 +296,51 @@ describe EasyAwscr::S3::Client do
283296
end
284297
end
285298
end
299+
300+
describe "allow to use your own awscr-s3 client" do
301+
describe "#constructor" do
302+
it "should use the client_provider if configured" do
303+
with_native_awscr_s3_client do |awscr_s3_client|
304+
call_count = 0
305+
client_provider = ->(_force_new : Bool) do
306+
call_count += 1
307+
awscr_s3_client
308+
end
309+
310+
client = EasyAwscr::S3::Client.new(client_provider: client_provider, region: "us-east-1", lazy_init: true)
311+
call_count.should eq(0)
312+
313+
list1 = client.list_buckets
314+
call_count.should eq(1)
315+
list2 = client.list_buckets
316+
call_count.should eq(2)
317+
318+
list1.buckets.should eq(list2.buckets)
319+
end
320+
end
321+
end
322+
323+
describe "#from_native_client" do
324+
it "should be support to wrap a single call" do
325+
with_native_awscr_s3_client do |awscr_s3_client|
326+
list1 = EasyAwscr::S3::Client.from_native_client(awscr_s3_client).list_buckets
327+
list2 = EasyAwscr::S3::Client.from_native_client(awscr_s3_client).list_buckets
328+
list1.buckets.should eq(list2.buckets)
329+
end
330+
end
331+
332+
it "should integrate with the normal client" do
333+
with_client do |client|
334+
with_temp_test_bucket(client) do |bucket|
335+
with_native_awscr_s3_client do |awscr_s3_client|
336+
native_client = EasyAwscr::S3::Client.from_native_client(awscr_s3_client)
337+
338+
client.head_bucket(bucket).should be_true
339+
native_client.head_bucket(bucket).should be_true
340+
end
341+
end
342+
end
343+
end
344+
end
345+
end
286346
end

src/easy-awscr/s3/client.cr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ module EasyAwscr::S3
77
@s3_client : Awscr::S3::Client?
88
@client_factory : Internals::ConnectionPool?
99

10+
# An optional hook for managing the native S3 client ("awscr-s3" library)
11+
# yourself, instead of letting "easy-aws" handle it internally.
12+
#
13+
# The first parameter is a hint whether the connections or credentials should
14+
# be refreshed. It is only a hint, and implementations are allowed to ignore it.
15+
alias ClientProvider = Proc(Bool, Awscr::S3::Client)
16+
1017
# This is a hard limit enforced by AWS for multipart uploads:
1118
# each part must be at least 5 MB.
1219
MINIMUM_PART_SIZE_5MB = 5242880
@@ -18,12 +25,18 @@ module EasyAwscr::S3
1825
def initialize(*,
1926
@region = EasyAwscr::Config.default_region!,
2027
@credential_provider = EasyAwscr::Config.default_credential_provider,
28+
@client_provider : ClientProvider? = nil,
2129
@endpoint : String? = nil,
2230
lazy_init = false)
2331
@mutex = Mutex.new(:unchecked)
2432
client unless lazy_init
2533
end
2634

35+
# Converts an existing client from "awscr-s3" to use the "easy-awscr" client interface.
36+
def self.from_native_client(native_client : Awscr::S3::Client) : self
37+
new(client_provider: ->(_force_new : Bool) { native_client }, region: "us-east-1", lazy_init: false)
38+
end
39+
2740
# Closes this client. If used again, a new connection will be opened.
2841
def close
2942
@mutex.synchronize do
@@ -288,6 +301,8 @@ module EasyAwscr::S3
288301
end
289302

290303
private def client(*, force_new = false) : Awscr::S3::Client
304+
@client_provider.try { |provider| return provider.call(force_new) }
305+
291306
dead_client_factory = nil
292307
@mutex.synchronize do
293308
s3_client = @s3_client

0 commit comments

Comments
 (0)