Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blob_exist? and container_exist? #11

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [Unreleased]

- Allow lazy loading the signer
- Add `blob_exist?`
- Add `container_exist?`

## [0.5.4] 2024-11-18

Expand Down
19 changes: 18 additions & 1 deletion lib/azure_blob/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand All @@ -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.
Expand Down Expand Up @@ -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]
Expand Down
21 changes: 21 additions & 0 deletions test/client/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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,
Expand Down
Loading