Skip to content

Commit a4f4672

Browse files
authored
Merge pull request #24 from testdouble/copy-across-containers
Add support for copying blobs between containers
2 parents 7b35f19 + ec02344 commit a4f4672

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

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

3+
- Add support for copying blobs across containers (#24)
4+
35
## [0.5.7.1] 2025-04-22
46

57
- Fixed a bug when reusing the account name in the container name. #22

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)