-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathstorage_configure_retries.py
63 lines (49 loc) · 2.28 KB
/
storage_configure_retries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# Copyright 2021 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
"""Sample that configures retries on an operation call.
This sample is used on this page:
https://cloud.google.com/storage/docs/retry-strategy
For more information, see README.md.
"""
# [START storage_configure_retries]
from google.cloud import storage
from google.cloud.storage.retry import DEFAULT_RETRY
def configure_retries(bucket_name, blob_name):
"""Configures retries with customizations."""
# The ID of your GCS bucket
# bucket_name = "your-bucket-name"
# The ID of your GCS object
# blob_name = "your-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)
# Customize retry with a deadline of 500 seconds (default=120 seconds).
modified_retry = DEFAULT_RETRY.with_deadline(500.0)
# Customize retry with an initial wait time of 1.5 (default=1.0).
# Customize retry with a wait time multiplier per iteration of 1.2 (default=2.0).
# Customize retry with a maximum wait time of 45.0 (default=60.0).
modified_retry = modified_retry.with_delay(initial=1.5, multiplier=1.2, maximum=45.0)
# blob.delete() uses DEFAULT_RETRY_IF_GENERATION_SPECIFIED by default.
# Override with modified_retry so the function retries even if the generation
# number is not specified.
print(
f"The following library method is customized to be retried according to the following configurations: {modified_retry}"
)
blob.delete(retry=modified_retry)
print(f"Blob {blob_name} deleted with a customized retry strategy.")
# [END storage_configure_retries]
if __name__ == "__main__":
configure_retries(bucket_name=sys.argv[1], blob_name=sys.argv[2])