diff --git a/storage/samples/snippets/snippets_test.py b/storage/samples/snippets/snippets_test.py index 1d3c8c1c442..b965f7c168e 100644 --- a/storage/samples/snippets/snippets_test.py +++ b/storage/samples/snippets/snippets_test.py @@ -667,7 +667,7 @@ def test_object_get_kms_key(test_bucket): assert kms_key.startswith(KMS_KEY) -def test_storage_compose_file(test_bucket): +def test_storage_compose_file(test_bucket, capsys): source_files = ["test_upload_blob_1", "test_upload_blob_2"] for source in source_files: blob = test_bucket.blob(source) @@ -684,6 +684,38 @@ def test_storage_compose_file(test_bucket): assert composed.decode("utf-8") == source_files[0] + source_files[1] + # Sources should not be deleted by default + assert test_bucket.blob(source_files[0]).exists() + assert test_bucket.blob(source_files[1]).exists() + + +def test_storage_compose_file_delete_sources(test_bucket, capsys): + source_files = ["test_upload_blob_3", "test_upload_blob_4"] + for source in source_files: + blob = test_bucket.blob(source) + blob.upload_from_string(source) + + 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] + + # Sources should be deleted + assert not test_bucket.blob(source_files[0]).exists() + assert not test_bucket.blob(source_files[1]).exists() + + out, _ = capsys.readouterr() + assert "Source objects {} and {} were deleted.".format( + source_files[0], source_files[1] + ) in out + def test_cors_configuration(test_bucket, capsys): bucket = storage_cors_configuration.cors_configuration(test_bucket) diff --git a/storage/samples/snippets/storage_compose_file.py b/storage/samples/snippets/storage_compose_file.py index e673912725b..2d199856fa7 100644 --- a/storage/samples/snippets/storage_compose_file.py +++ b/storage/samples/snippets/storage_compose_file.py @@ -20,7 +20,13 @@ 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" @@ -44,13 +50,25 @@ 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( + "Source objects {} and {} were deleted.".format( + first_blob_name, second_blob_name + ) + ) + return destination