From d76f79d77d4655225f46aab66075cc136a298376 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Wed, 24 Jun 2026 05:57:44 +0000 Subject: [PATCH 1/2] feat(storage): support delete source objects on compose Updates compose file sample to support deleting source objects optionally. Fixes b/441557254 [Generated-by: AI] --- .../samples/acceptance/files_test.rb | 20 +++++++++++++++++++ .../samples/storage_compose_file.rb | 8 +++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/google-cloud-storage/samples/acceptance/files_test.rb b/google-cloud-storage/samples/acceptance/files_test.rb index d1c6c53c74cf..d879bba7aa7a 100644 --- a/google-cloud-storage/samples/acceptance/files_test.rb +++ b/google-cloud-storage/samples/acceptance/files_test.rb @@ -431,6 +431,26 @@ def mock_cipher.random_key refute_nil bucket.file remote_file_name end + it "compose_file with delete_source_objects" do + file_1 = bucket.create_file local_file, file_1_name + file_2 = bucket.create_file local_file, file_2_name + + expected_out = "Composed new file #{remote_file_name} in the bucket #{bucket.name} " \ + "by combining #{file_1.name} and #{file_2.name}\n" \ + "Source objects were deleted\n" + assert_output expected_out do + compose_file bucket_name: bucket.name, + first_file_name: file_1.name, + second_file_name: file_2.name, + destination_file_name: remote_file_name, + delete_source_objects: true + end + + refute_nil bucket.file remote_file_name + assert_nil bucket.file file_1_name + assert_nil bucket.file file_2_name + end + it "copy_file" do bucket.create_file local_file, remote_file_name assert_nil secondary_bucket.file remote_file_name diff --git a/google-cloud-storage/samples/storage_compose_file.rb b/google-cloud-storage/samples/storage_compose_file.rb index 8b10228c2172..5fd1070a2a4c 100644 --- a/google-cloud-storage/samples/storage_compose_file.rb +++ b/google-cloud-storage/samples/storage_compose_file.rb @@ -13,7 +13,7 @@ # limitations under the License. # [START storage_compose_file] -def compose_file bucket_name:, first_file_name:, second_file_name:, destination_file_name: +def compose_file bucket_name:, first_file_name:, second_file_name:, destination_file_name:, delete_source_objects: false # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" @@ -31,12 +31,13 @@ def compose_file bucket_name:, first_file_name:, second_file_name:, destination_ storage = Google::Cloud::Storage.new bucket = storage.bucket bucket_name, skip_lookup: true - destination = bucket.compose [first_file_name, second_file_name], destination_file_name do |f| + destination = bucket.compose [first_file_name, second_file_name], destination_file_name, delete_source_objects: delete_source_objects do |f| f.content_type = "text/plain" end puts "Composed new file #{destination.name} in the bucket #{bucket_name} " \ "by combining #{first_file_name} and #{second_file_name}" + puts "Source objects were deleted" if delete_source_objects end # [END storage_compose_file] @@ -44,5 +45,6 @@ def compose_file bucket_name:, first_file_name:, second_file_name:, destination_ compose_file bucket_name: ARGV.shift, first_file_name: ARGV.shift, second_file_name: ARGV.shift, - destination_file_name: ARGV.shift + destination_file_name: ARGV.shift, + delete_source_objects: ARGV.shift == "true" end From 6b11df4ba4c535adf34e22c0d747f50df49679f0 Mon Sep 17 00:00:00 2001 From: Nidhi Nandwani Date: Fri, 26 Jun 2026 05:16:18 +0000 Subject: [PATCH 2/2] fix(storage): resolve PR comments for delete_source_objects samples Resolve comments in files_test.rb by adding assertions on source files and testing error scenarios. [Generated-by: AI] --- .../samples/acceptance/files_test.rb | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/google-cloud-storage/samples/acceptance/files_test.rb b/google-cloud-storage/samples/acceptance/files_test.rb index d879bba7aa7a..6cd9a7b1b3bc 100644 --- a/google-cloud-storage/samples/acceptance/files_test.rb +++ b/google-cloud-storage/samples/acceptance/files_test.rb @@ -429,6 +429,8 @@ def mock_cipher.random_key end refute_nil bucket.file remote_file_name + refute_nil bucket.file file_1_name + refute_nil bucket.file file_2_name end it "compose_file with delete_source_objects" do @@ -451,6 +453,30 @@ def mock_cipher.random_key assert_nil bucket.file file_2_name end + it "compose_file with delete_source_objects failing does not delete source objects" do + file_1 = bucket.create_file local_file, file_1_name + file_2 = bucket.create_file local_file, file_2_name + + real_storage = Google::Cloud::Storage.new + bucket.stub :compose, ->(*) { raise Google::Cloud::Error, "Mock API error" } do + real_storage.stub :bucket, bucket do + Google::Cloud::Storage.stub :new, real_storage do + assert_raises Google::Cloud::Error do + compose_file bucket_name: bucket.name, + first_file_name: file_1.name, + second_file_name: file_2.name, + destination_file_name: remote_file_name, + delete_source_objects: true + end + end + end + end + + refute_nil bucket.file file_1_name + refute_nil bucket.file file_2_name + assert_nil bucket.file remote_file_name + end + it "copy_file" do bucket.create_file local_file, remote_file_name assert_nil secondary_bucket.file remote_file_name