Skip to content

Commit 88e538d

Browse files
authored
Merge pull request #11 from testdouble/blob-exist
Add `blob_exist?` and `container_exist?`
2 parents 7a672f3 + 7fbc023 commit 88e538d

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.md

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

33
- Allow lazy loading the signer
4+
- Add `blob_exist?`
5+
- Add `container_exist?`
46

57
## [0.5.4] 2024-11-18
68

lib/azure_blob/client.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def list_blobs(options = {})
142142
#
143143
# Calls to {Get Blob Properties}[https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob-properties]
144144
#
145-
# This can be used to see if the blob exist or obtain metadata such as content type, disposition, checksum or Azure custom metadata.
145+
# This can be used to obtain metadata such as content type, disposition, checksum or Azure custom metadata.
146+
# To check for blob presence, look for `blob_exist?` as `get_blob_properties` raises on missing blob.
146147
def get_blob_properties(key, options = {})
147148
uri = generate_uri("#{container}/#{key}")
148149

@@ -151,6 +152,15 @@ def get_blob_properties(key, options = {})
151152
Blob.new(response)
152153
end
153154

155+
# Returns a boolean indicating if the blob exists.
156+
#
157+
# Calls to {Get Blob Properties}[https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob-properties]
158+
def blob_exist?(key, options = {})
159+
get_blob_properties(key, options).present?
160+
rescue AzureBlob::Http::FileNotFoundError
161+
false
162+
end
163+
154164
# Returns the tags associated with a blob
155165
#
156166
# Calls to the {Get Blob Tags}[https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob-tags] endpoint.
@@ -178,6 +188,13 @@ def get_container_properties(options = {})
178188
Container.new(response)
179189
end
180190

191+
# Returns a boolean indicating if the container exists.
192+
#
193+
# Calls to {Get Container Properties}[https://learn.microsoft.com/en-us/rest/api/storageservices/get-container-properties]
194+
def container_exist?(options = {})
195+
get_container_properties(options = {}).present?
196+
end
197+
181198
# Create the container
182199
#
183200
# Calls to {Create Container}[https://learn.microsoft.com/en-us/rest/api/storageservices/create-container]

test/client/test_client.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ def test_get_blob_properties_404
239239
assert_raises(AzureBlob::Http::FileNotFoundError) { client.get_blob_properties(key) }
240240
end
241241

242+
def test_blob_exist?
243+
refute client.blob_exist?(key)
244+
245+
client.create_block_blob(key, content)
246+
247+
assert client.blob_exist?(key)
248+
end
249+
242250
def test_append_blob
243251
client.create_append_blob(key)
244252
content.split("", 3).each { |chunk| client.append_blob_block(key, chunk) }
@@ -348,6 +356,19 @@ def test_get_container_properties
348356
refute container.present?
349357
end
350358

359+
def test_container_exist?
360+
assert client.container_exist?
361+
362+
client = AzureBlob::Client.new(
363+
account_name: @account_name,
364+
access_key: @access_key,
365+
container: "missingcontainer",
366+
principal_id: @principal_id,
367+
)
368+
369+
refute client.container_exist?
370+
end
371+
351372
def test_create_container
352373
client = AzureBlob::Client.new(
353374
account_name: @account_name,

0 commit comments

Comments
 (0)