diff --git a/CHANGELOG.md b/CHANGELOG.md index 0874f89..b046384 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## [Unreleased] - Allow lazy loading the signer +- Add `blob_exist?` +- Add `container_exist?` ## [0.5.4] 2024-11-18 diff --git a/lib/azure_blob/client.rb b/lib/azure_blob/client.rb index 7660517..21565d8 100644 --- a/lib/azure_blob/client.rb +++ b/lib/azure_blob/client.rb @@ -142,7 +142,8 @@ def list_blobs(options = {}) # # Calls to {Get Blob Properties}[https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob-properties] # - # This can be used to see if the blob exist or obtain metadata such as content type, disposition, checksum or Azure custom metadata. + # This can be used to obtain metadata such as content type, disposition, checksum or Azure custom metadata. + # To check for blob presence, look for `blob_exist?` as `get_blob_properties` raises on missing blob. def get_blob_properties(key, options = {}) uri = generate_uri("#{container}/#{key}") @@ -151,6 +152,15 @@ def get_blob_properties(key, options = {}) Blob.new(response) end + # Returns a boolean indicating if the blob exists. + # + # Calls to {Get Blob Properties}[https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob-properties] + def blob_exist?(key, options = {}) + get_blob_properties(key, options).present? + rescue AzureBlob::Http::FileNotFoundError + false + end + # Returns the tags associated with a blob # # 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 = {}) Container.new(response) end + # Returns a boolean indicating if the container exists. + # + # Calls to {Get Container Properties}[https://learn.microsoft.com/en-us/rest/api/storageservices/get-container-properties] + def container_exist?(options = {}) + get_container_properties(options = {}).present? + end + # Create the container # # Calls to {Create Container}[https://learn.microsoft.com/en-us/rest/api/storageservices/create-container] diff --git a/test/client/test_client.rb b/test/client/test_client.rb index a295f0e..2d29877 100644 --- a/test/client/test_client.rb +++ b/test/client/test_client.rb @@ -239,6 +239,14 @@ def test_get_blob_properties_404 assert_raises(AzureBlob::Http::FileNotFoundError) { client.get_blob_properties(key) } end + def test_blob_exist? + refute client.blob_exist?(key) + + client.create_block_blob(key, content) + + assert client.blob_exist?(key) + end + def test_append_blob client.create_append_blob(key) content.split("", 3).each { |chunk| client.append_blob_block(key, chunk) } @@ -348,6 +356,19 @@ def test_get_container_properties refute container.present? end + def test_container_exist? + assert client.container_exist? + + client = AzureBlob::Client.new( + account_name: @account_name, + access_key: @access_key, + container: "missingcontainer", + principal_id: @principal_id, + ) + + refute client.container_exist? + end + def test_create_container client = AzureBlob::Client.new( account_name: @account_name,