Skip to content

Commit f9701b2

Browse files
committed
feat(storage): support deleteSourceObjects option in compose sample
Add deleteSourceObjects parameter to compose sample. Updated test to assert both delete behavior scenarios. [Generated-by: AI]
1 parent 52bf097 commit f9701b2

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

storage/samples/snippets/snippets_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ def test_storage_compose_file(test_bucket):
673673
blob = test_bucket.blob(source)
674674
blob.upload_from_string(source)
675675

676+
# Test with delete_source_objects = False (default)
676677
with tempfile.NamedTemporaryFile() as dest_file:
677678
destination = storage_compose_file.compose_file(
678679
test_bucket.name,
@@ -683,6 +684,29 @@ def test_storage_compose_file(test_bucket):
683684
composed = destination.download_as_bytes()
684685

685686
assert composed.decode("utf-8") == source_files[0] + source_files[1]
687+
assert test_bucket.blob(source_files[0]).exists()
688+
assert test_bucket.blob(source_files[1]).exists()
689+
690+
# Clean up destination file
691+
destination.delete()
692+
693+
# Test with delete_source_objects = True
694+
with tempfile.NamedTemporaryFile() as dest_file:
695+
destination = storage_compose_file.compose_file(
696+
test_bucket.name,
697+
source_files[0],
698+
source_files[1],
699+
dest_file.name,
700+
delete_source_objects=True,
701+
)
702+
composed = destination.download_as_bytes()
703+
704+
assert composed.decode("utf-8") == source_files[0] + source_files[1]
705+
assert not test_bucket.blob(source_files[0]).exists()
706+
assert not test_bucket.blob(source_files[1]).exists()
707+
708+
# Clean up destination file
709+
destination.delete()
686710

687711

688712
def test_cors_configuration(test_bucket, capsys):

storage/samples/snippets/storage_compose_file.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
from google.cloud import storage
2121

2222

23-
def compose_file(bucket_name, first_blob_name, second_blob_name, destination_blob_name):
23+
def compose_file(
24+
bucket_name,
25+
first_blob_name,
26+
second_blob_name,
27+
destination_blob_name,
28+
delete_source_objects=False,
29+
):
2430
"""Concatenate source blobs into destination blob."""
2531
# bucket_name = "your-bucket-name"
2632
# first_blob_name = "first-object-name"
2733
# second_blob_name = "second-blob-name"
2834
# destination_blob_name = "destination-object-name"
35+
# delete_source_objects = False
2936

3037
storage_client = storage.Client()
3138
bucket = storage_client.bucket(bucket_name)
@@ -44,13 +51,24 @@ def compose_file(bucket_name, first_blob_name, second_blob_name, destination_blo
4451
# There is also an `if_source_generation_match` parameter, which is not used in this example.
4552
destination_generation_match_precondition = 0
4653

47-
destination.compose(sources, if_generation_match=destination_generation_match_precondition)
54+
destination.compose(
55+
sources,
56+
if_generation_match=destination_generation_match_precondition,
57+
delete_source_objects=delete_source_objects,
58+
)
4859

49-
print(
50-
"New composite object {} in the bucket {} was created by combining {} and {}".format(
51-
destination_blob_name, bucket_name, first_blob_name, second_blob_name
60+
if delete_source_objects:
61+
print(
62+
"New composite object {} in the bucket {} was created by combining {} and {}. Source objects were deleted.".format(
63+
destination_blob_name, bucket_name, first_blob_name, second_blob_name
64+
)
65+
)
66+
else:
67+
print(
68+
"New composite object {} in the bucket {} was created by combining {} and {}".format(
69+
destination_blob_name, bucket_name, first_blob_name, second_blob_name
70+
)
5271
)
53-
)
5472
return destination
5573

5674

0 commit comments

Comments
 (0)