Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions packages/google-cloud-storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -3849,6 +3849,7 @@ def compose(
if_metageneration_match=None,
if_source_generation_match=None,
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
delete_source_objects=None,
):
"""Concatenate source blobs into this one.

Expand Down Expand Up @@ -3908,6 +3909,11 @@ def compose(
Change the value to ``DEFAULT_RETRY`` or another `google.api_core.retry.Retry` object
to enable retries regardless of generation precondition setting.
See [Configuring Retries](https://cloud.google.com/python/docs/reference/storage/latest/retry_timeout).

:type delete_source_objects: bool
:param delete_source_objects:
(Optional) If True, the source objects will be deleted after a
successful composition.
"""
with create_trace_span(name="Storage.Blob.compose"):
sources_len = len(sources)
Expand Down Expand Up @@ -3964,6 +3970,9 @@ def compose(
"destination": self._properties.copy(),
}

if delete_source_objects is not None:
request["deleteSourceObjects"] = delete_source_objects

if self.user_project is not None:
query_params["userProject"] = self.user_project

Expand Down
20 changes: 20 additions & 0 deletions packages/google-cloud-storage/tests/system/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,26 @@ def test_blob_compose_new_blob(shared_bucket, blobs_to_delete):
assert destination.download_as_bytes() == payload_1 + payload_2


def test_blob_compose_delete_source_objects(shared_bucket):
payload_1 = b"AAA\n"
source_1 = shared_bucket.blob("source-1-delete")
source_1.upload_from_string(payload_1)

payload_2 = b"BBB\n"
source_2 = shared_bucket.blob("source-2-delete")
source_2.upload_from_string(payload_2)

destination = shared_bucket.blob("destination-delete")
destination.compose([source_1, source_2], delete_source_objects=True)

try:
assert destination.download_as_bytes() == payload_1 + payload_2
assert not source_1.exists()
assert not source_2.exists()
finally:
destination.delete()
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.

if assertion fails then destination will not be deleted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jules add the blobs_to_delete fixture and only append destination object, not the source objects. also make sure if assertion for source1/source2 fails, only then they should be added to the blobs_to_delete

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.

Added back the blobs_to_delete fixture. The code now appends the destination blob to blobs_to_delete. Source blobs are only added for cleanup if they still exist after the compose operation, ensuring they are deleted even if the primary assertion fails.



def test_blob_compose_new_blob_wo_content_type(shared_bucket, blobs_to_delete):
payload_1 = b"AAA\n"
source_1 = shared_bucket.blob("source-1")
Expand Down
39 changes: 39 additions & 0 deletions packages/google-cloud-storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -4480,6 +4480,45 @@ def test_compose_wo_content_type_set(self):
_target_object=destination,
)

def test_compose_w_delete_source_objects(self):
source_1_name = "source-1"
source_2_name = "source-2"
destination_name = "destination"
content_type = "text/plain"
delete_source_objects = True
api_response = {}
client = mock.Mock(spec=["_post_resource"])
client._post_resource.return_value = api_response
bucket = _Bucket(client=client)
source_1 = self._make_one(source_1_name, bucket=bucket)
source_2 = self._make_one(source_2_name, bucket=bucket)
destination = self._make_one(destination_name, bucket=bucket)
destination.content_type = content_type

destination.compose(
sources=[source_1, source_2],
delete_source_objects=delete_source_objects,
)

expected_path = f"/b/name/o/{destination_name}/compose"
expected_data = {
"sourceObjects": [
{"name": source_1.name, "generation": source_1.generation},
{"name": source_2.name, "generation": source_2.generation},
],
"destination": {"contentType": content_type},
"deleteSourceObjects": delete_source_objects,
}
expected_query_params = {}
client._post_resource.assert_called_once_with(
expected_path,
expected_data,
query_params=expected_query_params,
timeout=self._get_default_timeout(),
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
_target_object=destination,
)

def test_compose_minimal_w_user_project_w_timeout(self):
source_1_name = "source-1"
source_2_name = "source-2"
Expand Down
Loading