Skip to content

Add support for copying blobs between containers #24

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

Merged
merged 2 commits into from
May 15, 2025
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,5 +1,7 @@
## [Unreleased]

- Add support for copying blobs across containers (#24)

## [0.5.7.1] 2025-04-22

- Fixed a bug when reusing the account name in the container name. #22
Expand Down
12 changes: 9 additions & 3 deletions lib/azure_blob/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,22 @@ def get_blob(key, options = {})
Http.new(uri, headers, signer:).get
end

# Copy a blob
# Copy a blob between containers or within the same container
#
# Calls to {Copy Blob From URL}[https://learn.microsoft.com/en-us/rest/api/storageservices/copy-blob-from-url]
#
# Takes a key (path) and a source_key (path).
# Parameters:
# - key: destination blob path
# - source_key: source blob path
# - options: additional options
# - source_client: AzureBlob::Client instance for the source container (optional)
# If not provided, copies from within the same container
#
def copy_blob(key, source_key, options = {})
source_client = options.delete(:source_client) || self
uri = generate_uri("#{container}/#{key}")

source_uri = signed_uri(source_key, permissions: "r", expiry: Time.at(Time.now.to_i + 300).utc.iso8601)
source_uri = source_client.signed_uri(source_key, permissions: "r", expiry: Time.at(Time.now.to_i + 300).utc.iso8601)

headers = {
"x-ms-copy-source": source_uri.to_s,
Expand Down
23 changes: 23 additions & 0 deletions test/client/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def setup
@account_name = ENV["AZURE_ACCOUNT_NAME"]
@access_key = ENV["AZURE_ACCESS_KEY"]
@container = ENV["AZURE_PRIVATE_CONTAINER"]
@public_container = ENV["AZURE_PUBLIC_CONTAINER"]
@principal_id = ENV["AZURE_PRINCIPAL_ID"]
@host = ENV["STORAGE_BLOB_HOST"]
@client = AzureBlob::Client.new(
Expand Down Expand Up @@ -409,4 +410,26 @@ def test_get_blob_tags

assert_equal({ "tag1" => "value 1", "tag 2" => "value 2" }, tags)
end

def test_copy_between_containers
destination_client = AzureBlob::Client.new(
account_name: @account_name,
access_key: @access_key,
container: @public_container,
principal_id: @principal_id,
host: @host,
)
client.create_block_blob(key, content)
assert_equal content, client.get_blob(key)

destination_client.copy_blob(key, key, source_client: client)


assert_equal content, destination_client.get_blob(key)

begin
destination_client.delete_blob(key)
rescue AzureBlob::Http::FileNotFoundError
end
end
end