Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 173
feat: add *generation*match args into Blob.compose()#122
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
77bff6c
feat: add *generation*match args into Blob.compose()
818dc07
Merge branch 'master' into compose_generation_match
06b88be
add test case with error
d625940
Merge branch 'master' into compose_generation_match
frankyn 2ddda40
new compose surface
68f5cbe
Revert "new compose surface"
4320376
add an error for different length args, add a usage example
0dd16ba
Merge branch 'compose_generation_match' of https://github.com/q-logic…
79f6777
add condition to avoid sending params with None
dbc0266
specify comments
34c5142
Merge branch 'master' into compose_generation_match
frankyn 77e5889
Merge branch 'master' into compose_generation_match
da17481
Merge branch 'master' into compose_generation_match
frankyn 6cc8a6f
Merge branch 'master' into compose_generation_match
frankyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2234,34 +2234,104 @@ def make_private(self, client=None): | ||
| self.acl.all().revoke_read() | ||
| self.acl.save(client=client) | ||
| def compose(self, sources, client=None, timeout=_DEFAULT_TIMEOUT): | ||
| def compose( | ||
| self, | ||
| sources, | ||
| client=None, | ||
| timeout=_DEFAULT_TIMEOUT, | ||
| if_generation_match=None, | ||
| if_metageneration_match=None, | ||
| ): | ||
| """Concatenate source blobs into this one. | ||
| If :attr:`user_project` is set on the bucket, bills the API request | ||
| to that project. | ||
| :type sources: list of :class:`Blob` | ||
| :param sources: blobs whose contents will be composed into this blob. | ||
| :param sources: Blobs whose contents will be composed into this blob. | ||
| :type client: :class:`~google.cloud.storage.client.Client` or | ||
| ``NoneType`` | ||
| :param client: (Optional) The client to use. If not passed, falls back | ||
| :param client: (Optional) The client to use. If not passed, falls back | ||
| to the ``client`` stored on the blob's bucket. | ||
| :type timeout: float or tuple | ||
| :param timeout: (Optional) The amount of time, in seconds, to wait | ||
| for the server response. | ||
| Can also be passed as a tuple (connect_timeout, read_timeout). | ||
| See :meth:`requests.Session.request` documentation for details. | ||
| :type if_generation_match: list of long | ||
| :param if_generation_match: (Optional) Make the operation conditional on whether | ||
| the blob's current generation matches the given value. | ||
| Setting to 0 makes the operation succeed only if there | ||
| are no live versions of the blob. The list must match | ||
| ``sources`` item-to-item. | ||
| :type if_metageneration_match: list of long | ||
| :param if_metageneration_match: (Optional) Make the operation conditional on whether | ||
| the blob's current metageneration matches the given | ||
| value. The list must match ``sources`` item-to-item. | ||
| Example: | ||
| Compose blobs using generation match preconditions. | ||
| >>> from google.cloud import storage | ||
| >>> client = storage.Client() | ||
| >>> bucket = client.bucket("bucket-name") | ||
| >>> blobs = [bucket.blob("blob-name-1"), bucket.blob("blob-name-2")] | ||
| >>> if_generation_match = [None] * len(blobs) | ||
| >>> if_generation_match[0] = "123" # precondition for "blob-name-1" | ||
| >>> composed_blob = bucket.blob("composed-name") | ||
| >>> composed_blob.compose(blobs, if_generation_match) | ||
| """ | ||
| sources_len = len(sources) | ||
| if if_generation_match is not None and len(if_generation_match) != sources_len: | ||
| raise ValueError( | ||
| "'if_generation_match' length must be the same as 'sources' length" | ||
| ) | ||
| if ( | ||
| if_metageneration_match is not None | ||
| and len(if_metageneration_match) != sources_len | ||
| ): | ||
| raise ValueError( | ||
| "'if_metageneration_match' length must be the same as 'sources' length" | ||
| ) | ||
| client = self._require_client(client) | ||
| query_params = {} | ||
| if self.user_project is not None: | ||
| query_params["userProject"] = self.user_project | ||
| source_objects = [] | ||
| for index, source in enumerate(sources): | ||
| source_object = {"name": source.name} | ||
| preconditions = {} | ||
| if ( | ||
| if_generation_match is not None | ||
| and if_generation_match[index] is not None | ||
| ): | ||
| preconditions["ifGenerationMatch"] = if_generation_match[index] | ||
IlyaFaer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if ( | ||
| if_metageneration_match is not None | ||
| and if_metageneration_match[index] is not None | ||
| ): | ||
| preconditions["ifMetagenerationMatch"] = if_metageneration_match[index] | ||
| if preconditions: | ||
| source_object["objectPreconditions"] = preconditions | ||
| source_objects.append(source_object) | ||
| request = { | ||
| "sourceObjects": [{"name": source.name} for source in sources], | ||
| "sourceObjects": source_objects, | ||
| "destination": self._properties.copy(), | ||
| } | ||
| api_response = client._connection.api_request( | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1441,6 +1441,34 @@ def test_compose_replace_existing_blob(self): | ||
| composed = original.download_as_string() | ||
| self.assertEqual(composed, BEFORE + TO_APPEND) | ||
| def test_compose_with_generation_match(self): | ||
IlyaFaer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| BEFORE = b"AAA\n" | ||
| original = self.bucket.blob("original") | ||
| original.content_type = "text/plain" | ||
| original.upload_from_string(BEFORE) | ||
| self.case_blobs_to_delete.append(original) | ||
| TO_APPEND = b"BBB\n" | ||
| to_append = self.bucket.blob("to_append") | ||
| to_append.upload_from_string(TO_APPEND) | ||
| self.case_blobs_to_delete.append(to_append) | ||
| with self.assertRaises(google.api_core.exceptions.PreconditionFailed): | ||
| original.compose( | ||
| [original, to_append], | ||
| if_generation_match=[6, 7], | ||
| if_metageneration_match=[8, 9], | ||
| ) | ||
| original.compose( | ||
| [original, to_append], | ||
| if_generation_match=[original.generation, to_append.generation], | ||
| if_metageneration_match=[original.metageneration, to_append.metageneration], | ||
| ) | ||
| composed = original.download_as_string() | ||
| self.assertEqual(composed, BEFORE + TO_APPEND) | ||
| @unittest.skipUnless(USER_PROJECT, "USER_PROJECT not set in environment.") | ||
| def test_compose_with_user_project(self): | ||
| new_bucket_name = "compose-user-project" + unique_resource_id("-") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.