Skip to content

Commit 7a672f3

Browse files
authored
Merge pull request #7 from lucaong/lazy-signer-instantiation
Initialize signer lazily
2 parents 8be6834 + 4d5b925 commit 7a672f3

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## [Unreleased]
22

3+
- Allow lazy loading the signer
4+
35
## [0.5.4] 2024-11-18
46

57
- Allow creating public container

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ client.delete_blob(path)
9595

9696
For the full list of methods: https://www.rubydoc.info/gems/azure-blob/AzureBlob/Client
9797

98+
## options
99+
100+
### Lazy loading
101+
102+
The client is configured to raise an error early for missing credentials, causing it to crash before becoming healthy. This behavior can sometimes be undesirable, such as during assets precompilation.
103+
104+
To enable lazy loading and ignore missing credentials, set the `lazy` option:
105+
106+
`AzureBlob::Client.new(account_name: nil, access_key: nil, container: nil, lazy: true)`
107+
108+
or add `lazy: true` to your `config/storage.yml` for Active Storage.
109+
110+
98111
## Contributing
99112

100113
### Dev environment

lib/azure_blob/client.rb

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,10 @@ def initialize(account_name:, access_key: nil, principal_id: nil, container:, ho
2020
@container = container
2121
@host = host
2222
@cloud_regions = options[:cloud_regions]&.to_sym || :global
23-
24-
no_access_key = access_key.nil? || access_key&.empty?
25-
using_managed_identities = no_access_key && !principal_id.nil? || options[:use_managed_identities]
26-
27-
if !using_managed_identities && no_access_key
28-
raise AzureBlob::Error.new(
29-
"`access_key` cannot be empty. To use managed identities instead, pass a `principal_id` or set `use_managed_identities` to true."
30-
)
31-
end
32-
@signer = using_managed_identities ?
33-
AzureBlob::EntraIdSigner.new(account_name:, host: self.host, principal_id:) :
34-
AzureBlob::SharedKeySigner.new(account_name:, access_key:, host: self.host)
23+
@access_key = access_key
24+
@principal_id = principal_id
25+
@use_managed_identities = options[:use_managed_identities]
26+
signer unless options[:lazy]
3527
end
3628

3729
# Create a blob of type block. Will automatically split the the blob in multiple block and send the blob in pieces (blocks) if the blob is too big.
@@ -364,6 +356,24 @@ def host
364356
@host ||= "https://#{account_name}.blob.#{CLOUD_REGIONS_SUFFIX[cloud_regions]}"
365357
end
366358

367-
attr_reader :account_name, :signer, :container, :http, :cloud_regions
359+
def signer
360+
@signer ||=
361+
begin
362+
no_access_key = access_key.nil? || access_key&.empty?
363+
using_managed_identities = no_access_key && !principal_id.nil? || use_managed_identities
364+
365+
if !using_managed_identities && no_access_key
366+
raise AzureBlob::Error.new(
367+
"`access_key` cannot be empty. To use managed identities instead, pass a `principal_id` or set `use_managed_identities` to true."
368+
)
369+
end
370+
371+
using_managed_identities ?
372+
AzureBlob::EntraIdSigner.new(account_name:, host:, principal_id:) :
373+
AzureBlob::SharedKeySigner.new(account_name:, access_key:, host:)
374+
end
375+
end
376+
377+
attr_reader :account_name, :container, :http, :cloud_regions, :access_key, :principal_id, :use_managed_identities
368378
end
369379
end

test/client/test_client.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ def test_without_credentials
6666
)
6767
end
6868

69+
def test_lazy_loading_doesnt_raise_before_querying
70+
client = AzureBlob::Client.new(
71+
account_name: @account_name,
72+
container: @container,
73+
lazy: true,
74+
)
75+
76+
assert_raises(AzureBlob::Error) do
77+
client.create_block_blob(key, content)
78+
end
79+
end
80+
6981
def test_single_block_upload
7082
client.create_block_blob(key, content)
7183

0 commit comments

Comments
 (0)