Skip to content
Closed
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
24 changes: 24 additions & 0 deletions storage/samples/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ def test_storage_compose_file(test_bucket):
blob = test_bucket.blob(source)
blob.upload_from_string(source)

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

assert composed.decode("utf-8") == source_files[0] + source_files[1]
assert test_bucket.blob(source_files[0]).exists()
assert test_bucket.blob(source_files[1]).exists()

# Clean up destination file
destination.delete()

# Test with delete_source_objects = True
with tempfile.NamedTemporaryFile() as dest_file:
destination = storage_compose_file.compose_file(
test_bucket.name,
source_files[0],
source_files[1],
dest_file.name,
delete_source_objects=True,
)
composed = destination.download_as_bytes()

assert composed.decode("utf-8") == source_files[0] + source_files[1]
assert not test_bucket.blob(source_files[0]).exists()
assert not test_bucket.blob(source_files[1]).exists()

# Clean up destination file
destination.delete()
Comment on lines +694 to +709

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the previous test block, the destination blob cleanup should be moved inside the with block to keep the scope localized, and the comment updated to refer to 'destination blob' instead of 'destination file'.

Suggested change
with tempfile.NamedTemporaryFile() as dest_file:
destination = storage_compose_file.compose_file(
test_bucket.name,
source_files[0],
source_files[1],
dest_file.name,
delete_source_objects=True,
)
composed = destination.download_as_bytes()
assert composed.decode("utf-8") == source_files[0] + source_files[1]
assert not test_bucket.blob(source_files[0]).exists()
assert not test_bucket.blob(source_files[1]).exists()
# Clean up destination file
destination.delete()
with tempfile.NamedTemporaryFile() as dest_file:
destination = storage_compose_file.compose_file(
test_bucket.name,
source_files[0],
source_files[1],
dest_file.name,
delete_source_objects=True,
)
composed = destination.download_as_bytes()
assert composed.decode("utf-8") == source_files[0] + source_files[1]
assert not test_bucket.blob(source_files[0]).exists()
assert not test_bucket.blob(source_files[1]).exists()
# Clean up destination blob
destination.delete()



def test_cors_configuration(test_bucket, capsys):
Expand Down
30 changes: 24 additions & 6 deletions storage/samples/snippets/storage_compose_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
from google.cloud import storage


def compose_file(bucket_name, first_blob_name, second_blob_name, destination_blob_name):
def compose_file(
bucket_name,
first_blob_name,
second_blob_name,
destination_blob_name,
delete_source_objects=False,
):
"""Concatenate source blobs into destination blob."""
# bucket_name = "your-bucket-name"
# first_blob_name = "first-object-name"
# second_blob_name = "second-blob-name"
# destination_blob_name = "destination-object-name"
# delete_source_objects = False

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

destination.compose(sources, if_generation_match=destination_generation_match_precondition)
destination.compose(
sources,
if_generation_match=destination_generation_match_precondition,
delete_source_objects=delete_source_objects,
)

print(
"New composite object {} in the bucket {} was created by combining {} and {}".format(
destination_blob_name, bucket_name, first_blob_name, second_blob_name
if delete_source_objects:
print(
"New composite object {} in the bucket {} was created by combining {} and {}. Source objects were deleted.".format(
destination_blob_name, bucket_name, first_blob_name, second_blob_name
)
)
else:
print(
"New composite object {} in the bucket {} was created by combining {} and {}".format(
destination_blob_name, bucket_name, first_blob_name, second_blob_name
)
)
Comment on lines +60 to 71

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The print statements for delete_source_objects contain duplicated text. We can simplify this by constructing the base message first and conditionally appending the deletion status. This reduces duplication and improves maintainability.

    message = "New composite object {} in the bucket {} was created by combining {} and {}".format(
        destination_blob_name, bucket_name, first_blob_name, second_blob_name
    )
    if delete_source_objects:
        message += " Source objects were deleted."
    print(message)

)
return destination


Expand Down
Loading