Skip to content

Commit a28b39c

Browse files
authored
samples: add metageneration-match preconditions to samples (#975)
* samples: add metageneration-match preconditions to samples * update optional block wording and flow
1 parent 6279b2a commit a28b39c

5 files changed

Lines changed: 40 additions & 5 deletions

File tree

packages/google-cloud-storage/samples/snippets/storage_release_event_based_hold.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ def release_event_based_hold(bucket_name, blob_name):
2929
storage_client = storage.Client()
3030
bucket = storage_client.bucket(bucket_name)
3131
blob = bucket.blob(blob_name)
32+
metageneration_match_precondition = None
33+
34+
# Optional: set a metageneration-match precondition to avoid potential race
35+
# conditions and data corruptions. The request to patch is aborted if the
36+
# object's metageneration does not match your precondition.
37+
blob.reload() # Fetch blob metadata to use in metageneration_match_precondition.
38+
metageneration_match_precondition = blob.metageneration
3239

3340
blob.event_based_hold = False
34-
blob.patch()
41+
blob.patch(if_metageneration_match=metageneration_match_precondition)
3542

3643
print(f"Event based hold was released for {blob_name}")
3744

packages/google-cloud-storage/samples/snippets/storage_release_temporary_hold.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ def release_temporary_hold(bucket_name, blob_name):
2929
storage_client = storage.Client()
3030
bucket = storage_client.bucket(bucket_name)
3131
blob = bucket.blob(blob_name)
32+
metageneration_match_precondition = None
33+
34+
# Optional: set a metageneration-match precondition to avoid potential race
35+
# conditions and data corruptions. The request to patch is aborted if the
36+
# object's metageneration does not match your precondition.
37+
blob.reload() # Fetch blob metadata to use in metageneration_match_precondition.
38+
metageneration_match_precondition = blob.metageneration
3239

3340
blob.temporary_hold = False
34-
blob.patch()
41+
blob.patch(if_metageneration_match=metageneration_match_precondition)
3542

3643
print("Temporary hold was release for #{blob_name}")
3744

packages/google-cloud-storage/samples/snippets/storage_set_event_based_hold.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ def set_event_based_hold(bucket_name, blob_name):
2828
storage_client = storage.Client()
2929
bucket = storage_client.bucket(bucket_name)
3030
blob = bucket.blob(blob_name)
31+
metageneration_match_precondition = None
32+
33+
# Optional: set a metageneration-match precondition to avoid potential race
34+
# conditions and data corruptions. The request to patch is aborted if the
35+
# object's metageneration does not match your precondition.
36+
blob.reload() # Fetch blob metadata to use in metageneration_match_precondition.
37+
metageneration_match_precondition = blob.metageneration
3138

3239
blob.event_based_hold = True
33-
blob.patch()
40+
blob.patch(if_metageneration_match=metageneration_match_precondition)
3441

3542
print(f"Event based hold was set for {blob_name}")
3643

packages/google-cloud-storage/samples/snippets/storage_set_metadata.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ def set_blob_metadata(bucket_name, blob_name):
2828
storage_client = storage.Client()
2929
bucket = storage_client.bucket(bucket_name)
3030
blob = bucket.get_blob(blob_name)
31+
metageneration_match_precondition = None
32+
33+
# Optional: set a metageneration-match precondition to avoid potential race
34+
# conditions and data corruptions. The request to patch is aborted if the
35+
# object's metageneration does not match your precondition.
36+
metageneration_match_precondition = blob.metageneration
37+
3138
metadata = {'color': 'Red', 'name': 'Test'}
3239
blob.metadata = metadata
33-
blob.patch()
40+
blob.patch(if_metageneration_match=metageneration_match_precondition)
3441

3542
print(f"The metadata for the blob {blob.name} is {blob.metadata}")
3643

packages/google-cloud-storage/samples/snippets/storage_set_temporary_hold.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ def set_temporary_hold(bucket_name, blob_name):
2828
storage_client = storage.Client()
2929
bucket = storage_client.bucket(bucket_name)
3030
blob = bucket.blob(blob_name)
31+
metageneration_match_precondition = None
32+
33+
# Optional: set a metageneration-match precondition to avoid potential race
34+
# conditions and data corruptions. The request to patch is aborted if the
35+
# object's metageneration does not match your precondition.
36+
blob.reload() # Fetch blob metadata to use in metageneration_match_precondition.
37+
metageneration_match_precondition = blob.metageneration
3138

3239
blob.temporary_hold = True
33-
blob.patch()
40+
blob.patch(if_metageneration_match=metageneration_match_precondition)
3441

3542
print("Temporary hold was set for #{blob_name}")
3643

0 commit comments

Comments
 (0)