Skip to content

Commit 6999370

Browse files
authored
docs: streamline docstrings for conditional parmas (#464)
Add narrative docs for conditional params: - 'if_generation_match' - 'if_generation_not_match' - 'if_metageneration_match' - 'if_metageneration_not_match' Streamline docstrings for conditional params, linking to new narrative docs. Also, move 'timeout' argument to end (just before 'retry', if present) for consistency. Closes #460.
1 parent cf22a11 commit 6999370

File tree

9 files changed

+505
-494
lines changed

9 files changed

+505
-494
lines changed

docs/generation_metageneration.rst

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Conditional Requests Via Generation / Metageneration Preconditions
2+
==================================================================
3+
4+
Preconditions tell Cloud Storage to only perform a request if the
5+
:ref:`generation <concept-generation>` or
6+
:ref:`metageneration <concept-metageneration>` number of the affected object
7+
meets your precondition criteria. These checks of the generation and
8+
metageneration numbers ensure that the object is in the expected state,
9+
allowing you to perform safe read-modify-write updates and conditional
10+
operations on objects
11+
12+
Concepts
13+
--------
14+
15+
.. _concept-metageneration:
16+
17+
Metageneration
18+
::::::::::::::
19+
20+
When you create a :class:`~google.cloud.storage.bucket.Bucket`,
21+
its :attr:`~google.cloud.storage.bucket.Bucket.metageneration` is initialized
22+
to ``1``, representing the initial version of the bucket's metadata.
23+
24+
When you first upload a
25+
:class:`~google.cloud.storage.blob.Blob` ("Object" in the GCS back-end docs),
26+
its :attr:`~google.cloud.storage.blob.Blob.metageneration` is likewise
27+
initialized to ``1``. representing the initial version of the blob's metadata.
28+
29+
The ``metageneration`` attribute is set by the GCS back-end, and is read-only
30+
in the client library.
31+
32+
Each time you patch or update the bucket's / blob's metadata, its
33+
``metageneration`` is incremented.
34+
35+
36+
.. _concept-generation:
37+
38+
Generation
39+
::::::::::
40+
41+
Each time you upload a new version of a file to a
42+
:class:`~google.cloud.storage.blob.Blob` ("Object" in the GCS back-end docs),
43+
the Blob's :attr:`~google.cloud.storage.blob.generation` is changed, and its
44+
:attr:`~google.cloud.storage.blob.metageneration` is reset to ``1`` (the first
45+
metadata version for that generation of the blob).
46+
47+
The ``generation`` attribute is set by the GCS back-end, and is read-only
48+
in the client library.
49+
50+
See also
51+
::::::::
52+
53+
- `Storage API Generation Precondition docs`_
54+
55+
.. _Storage API Generation Precondition docs:
56+
https://cloud.google.com/storage/docs/generations-preconditions
57+
58+
59+
Conditional Parameters
60+
----------------------
61+
62+
.. _using-if-generation-match:
63+
64+
Using ``if_generation_match``
65+
:::::::::::::::::::::::::::::
66+
67+
Passing the ``if_generation_match`` parameter to a method which retrieves a
68+
blob resource (e.g.,
69+
:meth:`Blob.reload <google.cloud.storage.blob.Blob.reload>`) or modifies
70+
the blob (e.g.,
71+
:meth:`Blob.update <google.cloud.storage.blob.Blob.update>`)
72+
makes the operation conditional on whether the blob's current ``generation``
73+
matches the given value.
74+
75+
As a special case, passing ``0`` as the value for``if_generation_match``
76+
makes the operation succeed only if there are no live versions of the blob.
77+
78+
79+
.. _using-if-generation-not-match:
80+
81+
Using ``if_generation_not_match``
82+
:::::::::::::::::::::::::::::::::
83+
84+
Passing the ``if_generation_not_match`` parameter to a method which retrieves
85+
a blob resource (e.g.,
86+
:meth:`Blob.reload <google.cloud.storage.blob.Blob.reload>`) or modifies
87+
the blob (e.g.,
88+
:meth:`Blob.update <google.cloud.storage.blob.Blob.update>`)
89+
makes the operation conditional on whether the blob's current ``generation``
90+
does **not** match the given value.
91+
92+
If no live version of the blob exists, the precondition fails.
93+
94+
As a special case, passing ``0`` as the value for ``if_generation_not_match``
95+
makes the operation succeed only if there **is** a live version of the blob.
96+
97+
98+
.. _using-if-metageneration-match:
99+
100+
Using ``if_metageneration_match``
101+
:::::::::::::::::::::::::::::::::
102+
103+
Passing the ``if_metageneration_match`` parameter to a method which retrieves
104+
a blob or bucket resource
105+
(e.g., :meth:`Blob.reload <google.cloud.storage.blob.Blob.reload>`,
106+
:meth:`Bucket.reload <google.cloud.storage.bucket.Bucket.reload>`)
107+
or modifies the blob or bucket (e.g.,
108+
:meth:`Blob.update <google.cloud.storage.blob.Blob.update>`
109+
:meth:`Bucket.patch <google.cloud.storage.bucket.Bucket.patch>`)
110+
makes the operation conditional on whether the resource's current
111+
``metageneration`` matches the given value.
112+
113+
114+
.. _using-if-metageneration-not-match:
115+
116+
Using ``if_metageneration_not_match``
117+
:::::::::::::::::::::::::::::::::::::
118+
119+
Passing the ``if_metageneration_not_match`` parameter to a method which
120+
retrieves a blob or bucket resource
121+
(e.g., :meth:`Blob.reload <google.cloud.storage.blob.Blob.reload>`,
122+
:meth:`Bucket.reload <google.cloud.storage.bucket.Bucket.reload>`)
123+
or modifies the blob or bucket (e.g.,
124+
:meth:`Blob.update <google.cloud.storage.blob.Blob.update>`
125+
:meth:`Bucket.patch <google.cloud.storage.bucket.Bucket.patch>`)
126+
makes the operation conditional on whether the resource's current
127+
``metageneration`` does **not** match the given value.

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ API Reference
2222
hmac_key
2323
notification
2424
retry_timeout
25+
generation_metageneration
2526

2627
Changelog
2728
---------

google/cloud/storage/_helpers.py

+42-57
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ def reload(
147147
self,
148148
client=None,
149149
projection="noAcl",
150-
timeout=_DEFAULT_TIMEOUT,
151150
if_generation_match=None,
152151
if_generation_not_match=None,
153152
if_metageneration_match=None,
154153
if_metageneration_not_match=None,
154+
timeout=_DEFAULT_TIMEOUT,
155155
retry=DEFAULT_RETRY,
156156
):
157157
"""Reload properties from Cloud Storage.
@@ -168,31 +168,26 @@ def reload(
168168
Defaults to ``'noAcl'``. Specifies the set of
169169
properties to return.
170170
171-
:type timeout: float or tuple
172-
:param timeout:
173-
(Optional) The amount of time, in seconds, to wait
174-
for the server response. See: :ref:`configuring_timeouts`
175-
176171
:type if_generation_match: long
177-
:param if_generation_match: (Optional) Make the operation conditional on whether
178-
the blob's current generation matches the given value.
179-
Setting to 0 makes the operation succeed only if there
180-
are no live versions of the blob.
172+
:param if_generation_match:
173+
(Optional) See :ref:`using-if-generation-match`
181174
182175
:type if_generation_not_match: long
183-
:param if_generation_not_match: (Optional) Make the operation conditional on whether
184-
the blob's current generation does not match the given
185-
value. If no live blob exists, the precondition fails.
186-
Setting to 0 makes the operation succeed only if there
187-
is a live version of the blob.
176+
:param if_generation_not_match:
177+
(Optional) See :ref:`using-if-generation-not-match`
188178
189179
:type if_metageneration_match: long
190-
:param if_metageneration_match: (Optional) Make the operation conditional on whether the
191-
blob's current metageneration matches the given value.
180+
:param if_metageneration_match:
181+
(Optional) See :ref:`using-if-metageneration-match`
192182
193183
:type if_metageneration_not_match: long
194-
:param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
195-
blob's current metageneration does not match the given value.
184+
:param if_metageneration_not_match:
185+
(Optional) See :ref:`using-if-metageneration-not-match`
186+
187+
:type timeout: float or tuple
188+
:param timeout:
189+
(Optional) The amount of time, in seconds, to wait
190+
for the server response. See: :ref:`configuring_timeouts`
196191
197192
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
198193
:param retry:
@@ -251,11 +246,11 @@ def _set_properties(self, value):
251246
def patch(
252247
self,
253248
client=None,
254-
timeout=_DEFAULT_TIMEOUT,
255249
if_generation_match=None,
256250
if_generation_not_match=None,
257251
if_metageneration_match=None,
258252
if_metageneration_not_match=None,
253+
timeout=_DEFAULT_TIMEOUT,
259254
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
260255
):
261256
"""Sends all changed properties in a PATCH request.
@@ -269,31 +264,26 @@ def patch(
269264
:param client: the client to use. If not passed, falls back to the
270265
``client`` stored on the current object.
271266
272-
:type timeout: float or tuple
273-
:param timeout:
274-
(Optional) The amount of time, in seconds, to wait
275-
for the server response. See: :ref:`configuring_timeouts`
276-
277267
:type if_generation_match: long
278-
:param if_generation_match: (Optional) Make the operation conditional on whether
279-
the blob's current generation matches the given value.
280-
Setting to 0 makes the operation succeed only if there
281-
are no live versions of the blob.
268+
:param if_generation_match:
269+
(Optional) See :ref:`using-if-generation-match`
282270
283271
:type if_generation_not_match: long
284-
:param if_generation_not_match: (Optional) Make the operation conditional on whether
285-
the blob's current generation does not match the given
286-
value. If no live blob exists, the precondition fails.
287-
Setting to 0 makes the operation succeed only if there
288-
is a live version of the blob.
272+
:param if_generation_not_match:
273+
(Optional) See :ref:`using-if-generation-not-match`
289274
290275
:type if_metageneration_match: long
291-
:param if_metageneration_match: (Optional) Make the operation conditional on whether the
292-
blob's current metageneration matches the given value.
276+
:param if_metageneration_match:
277+
(Optional) See :ref:`using-if-metageneration-match`
293278
294279
:type if_metageneration_not_match: long
295-
:param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
296-
blob's current metageneration does not match the given value.
280+
:param if_metageneration_not_match:
281+
(Optional) See :ref:`using-if-metageneration-not-match`
282+
283+
:type timeout: float or tuple
284+
:param timeout:
285+
(Optional) The amount of time, in seconds, to wait
286+
for the server response. See: :ref:`configuring_timeouts`
297287
298288
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
299289
:param retry:
@@ -327,11 +317,11 @@ def patch(
327317
def update(
328318
self,
329319
client=None,
330-
timeout=_DEFAULT_TIMEOUT,
331320
if_generation_match=None,
332321
if_generation_not_match=None,
333322
if_metageneration_match=None,
334323
if_metageneration_not_match=None,
324+
timeout=_DEFAULT_TIMEOUT,
335325
retry=DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
336326
):
337327
"""Sends all properties in a PUT request.
@@ -345,31 +335,26 @@ def update(
345335
:param client: the client to use. If not passed, falls back to the
346336
``client`` stored on the current object.
347337
348-
:type timeout: float or tuple
349-
:param timeout:
350-
(Optional) The amount of time, in seconds, to wait
351-
for the server response. See: :ref:`configuring_timeouts`
352-
353338
:type if_generation_match: long
354-
:param if_generation_match: (Optional) Make the operation conditional on whether
355-
the blob's current generation matches the given value.
356-
Setting to 0 makes the operation succeed only if there
357-
are no live versions of the blob.
339+
:param if_generation_match:
340+
(Optional) See :ref:`using-if-generation-match`
358341
359342
:type if_generation_not_match: long
360-
:param if_generation_not_match: (Optional) Make the operation conditional on whether
361-
the blob's current generation does not match the given
362-
value. If no live blob exists, the precondition fails.
363-
Setting to 0 makes the operation succeed only if there
364-
is a live version of the blob.
343+
:param if_generation_not_match:
344+
(Optional) See :ref:`using-if-generation-not-match`
365345
366346
:type if_metageneration_match: long
367-
:param if_metageneration_match: (Optional) Make the operation conditional on whether the
368-
blob's current metageneration matches the given value.
347+
:param if_metageneration_match:
348+
(Optional) See :ref:`using-if-metageneration-match`
369349
370350
:type if_metageneration_not_match: long
371-
:param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
372-
blob's current metageneration does not match the given value.
351+
:param if_metageneration_not_match:
352+
(Optional) See :ref:`using-if-metageneration-not-match`
353+
354+
:type timeout: float or tuple
355+
:param timeout:
356+
(Optional) The amount of time, in seconds, to wait
357+
for the server response. See: :ref:`configuring_timeouts`
373358
374359
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
375360
:param retry:

0 commit comments

Comments
 (0)