Skip to content

Commit 4aaea4c

Browse files
committed
Add support for copying blobs between containers
Fixes #23
1 parent da34bf7 commit 4aaea4c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/azure_blob/client.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,22 @@ def get_blob(key, options = {})
7777
Http.new(uri, headers, signer:).get
7878
end
7979

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

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

9197
headers = {
9298
"x-ms-copy-source": source_uri.to_s,

test/client/test_client.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def setup
1212
@account_name = ENV["AZURE_ACCOUNT_NAME"]
1313
@access_key = ENV["AZURE_ACCESS_KEY"]
1414
@container = ENV["AZURE_PRIVATE_CONTAINER"]
15+
@public_container = ENV["AZURE_PUBLIC_CONTAINER"]
1516
@principal_id = ENV["AZURE_PRINCIPAL_ID"]
1617
@host = ENV["STORAGE_BLOB_HOST"]
1718
@client = AzureBlob::Client.new(
@@ -409,4 +410,26 @@ def test_get_blob_tags
409410

410411
assert_equal({ "tag1" => "value 1", "tag 2" => "value 2" }, tags)
411412
end
413+
414+
def test_copy_between_containers
415+
destination_client = AzureBlob::Client.new(
416+
account_name: @account_name,
417+
access_key: @access_key,
418+
container: @public_container,
419+
principal_id: @principal_id,
420+
host: @host,
421+
)
422+
client.create_block_blob(key, content)
423+
assert_equal content, client.get_blob(key)
424+
425+
destination_client.copy_blob(key, key, source_client: client)
426+
427+
428+
assert_equal content, destination_client.get_blob(key)
429+
430+
begin
431+
destination_client.delete_blob(key)
432+
rescue AzureBlob::Http::FileNotFoundError
433+
end
434+
end
412435
end

0 commit comments

Comments
 (0)