Application developers can override the defaultretryandbackoffpolicies:
namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](std::string const& bucket_name, std::string const& object_name,
std::string const& contents) {
// Create a client that only gives up on the third error. The default policy
// is to retry for several minutes.
auto client =
gcs::Client(google::cloud::Options{}.set<gcs::RetryPolicyOption>(
gcs::LimitedErrorCountRetryPolicy(3).clone()));
StatusOr<gcs::ObjectMetadata> object_metadata =
client.InsertObject(bucket_name, object_name, std::move(contents),
gcs::IfGenerationMatch(0));
if (!object_metadata) throw std::move(object_metadata).status();
std::cout << "The object " << object_metadata->name()
<< " was created in bucket " << object_metadata->bucket()
<< "\nFull metadata: " << *object_metadata << "\n";
}
The default policies are to continue retrying for up to 15 minutes, and to use truncated (at 5 minutes) exponential backoff, doubling the maximum backoff period between retries.
By default the library retries all operations, even those that are not idempotent. Application developers can override theidempotency policyto follow a more conservative approach:
namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](std::string const& bucket_name, std::string const& object_name,
std::string const& contents) {
// Create a client that only retries idempotent operations, the default is
// to retry all operations.
auto client =
gcs::Client(google::cloud::Options{}.set<gcs::IdempotencyPolicyOption>(
gcs::StrictIdempotencyPolicy().clone()));
StatusOr<gcs::ObjectMetadata> object_metadata =
client.InsertObject(bucket_name, object_name, std::move(contents),
gcs::IfGenerationMatch(0));
if (!object_metadata) throw std::move(object_metadata).status();
std::cout << "The object " << object_metadata->name()
<< " was created in bucket " << object_metadata->bucket()
<< "\nFull metadata: " << *object_metadata << "\n";
}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThe latest version available is 2.37.0-rc, with multiple older versions, ranging from 2.36.0 down to 2.11.0, also accessible.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can customize retry policies by overriding the default \u003ccode\u003eRetryPolicy\u003c/code\u003e and \u003ccode\u003eBackoff\u003c/code\u003e policies, allowing adjustments to retry limits and backoff periods.\u003c/p\u003e\n"],["\u003cp\u003eBy default the library retries all operations, but developers can also adjust the \u003ccode\u003eIdempotencyPolicy\u003c/code\u003e to retry only idempotent operations, providing more control over retry behavior.\u003c/p\u003e\n"],["\u003cp\u003eAlternative retry policies like \u003ccode\u003eLimitedTimeRetryPolicy\u003c/code\u003e and \u003ccode\u003eLimitedErrorCountRetryPolicy\u003c/code\u003e, alongside alternative idempotency policies like \u003ccode\u003eAlwaysRetryIdempotencyPolicy\u003c/code\u003e and \u003ccode\u003eStrictIdempotencyPolicy\u003c/code\u003e, are available for further customization.\u003c/p\u003e\n"],["\u003cp\u003eThe default policies are to continue retrying for up to 15 minutes, and to use truncated (at 5 minutes) exponential backoff, doubling the maximum backoff period between retries.\u003c/p\u003e\n"]]],[],null,["Version 2.20.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/storage/latest/storage-retry-examples)\n- [2.41.0](/cpp/docs/reference/storage/2.41.0/storage-retry-examples)\n- [2.40.0](/cpp/docs/reference/storage/2.40.0/storage-retry-examples)\n- [2.39.0](/cpp/docs/reference/storage/2.39.0/storage-retry-examples)\n- [2.38.0](/cpp/docs/reference/storage/2.38.0/storage-retry-examples)\n- [2.37.0](/cpp/docs/reference/storage/2.37.0/storage-retry-examples)\n- [2.36.0](/cpp/docs/reference/storage/2.36.0/storage-retry-examples)\n- [2.35.0](/cpp/docs/reference/storage/2.35.0/storage-retry-examples)\n- [2.34.0](/cpp/docs/reference/storage/2.34.0/storage-retry-examples)\n- [2.33.0](/cpp/docs/reference/storage/2.33.0/storage-retry-examples)\n- [2.32.0](/cpp/docs/reference/storage/2.32.0/storage-retry-examples)\n- [2.31.0](/cpp/docs/reference/storage/2.31.0/storage-retry-examples)\n- [2.30.0](/cpp/docs/reference/storage/2.30.0/storage-retry-examples)\n- [2.29.0](/cpp/docs/reference/storage/2.29.0/storage-retry-examples)\n- [2.28.0](/cpp/docs/reference/storage/2.28.0/storage-retry-examples)\n- [2.27.0](/cpp/docs/reference/storage/2.27.0/storage-retry-examples)\n- [2.26.0](/cpp/docs/reference/storage/2.26.0/storage-retry-examples)\n- [2.25.1](/cpp/docs/reference/storage/2.25.1/storage-retry-examples)\n- [2.24.0](/cpp/docs/reference/storage/2.24.0/storage-retry-examples)\n- [2.23.0](/cpp/docs/reference/storage/2.23.0/storage-retry-examples)\n- [2.22.1](/cpp/docs/reference/storage/2.22.1/storage-retry-examples)\n- [2.21.0](/cpp/docs/reference/storage/2.21.0/storage-retry-examples)\n- [2.20.0](/cpp/docs/reference/storage/2.20.0/storage-retry-examples)\n- [2.19.0](/cpp/docs/reference/storage/2.19.0/storage-retry-examples)\n- [2.18.0](/cpp/docs/reference/storage/2.18.0/storage-retry-examples)\n- [2.17.0](/cpp/docs/reference/storage/2.17.0/storage-retry-examples)\n- [2.16.0](/cpp/docs/reference/storage/2.16.0/storage-retry-examples)\n- [2.15.1](/cpp/docs/reference/storage/2.15.1/storage-retry-examples)\n- [2.14.0](/cpp/docs/reference/storage/2.14.0/storage-retry-examples)\n- [2.13.0](/cpp/docs/reference/storage/2.13.0/storage-retry-examples)\n- [2.12.0](/cpp/docs/reference/storage/2.12.0/storage-retry-examples)\n- [2.11.0](/cpp/docs/reference/storage/2.11.0/storage-retry-examples) \n\nOverride the default retry policies\n===================================\n\nApplication developers can override the default [retry](/cpp/docs/reference/storage/2.20.0/classgoogle_1_1cloud_1_1storage_1_1RetryPolicy) and [backoff](/cpp/docs/reference/storage/2.20.0/namespacegoogle_1_1cloud_1_1storage) policies: \n\n namespace gcs = ::google::cloud::storage;\n using ::google::cloud::StatusOr;\n [](std::string const& bucket_name, std::string const& object_name,\n std::string const& contents) {\n // Create a client that only gives up on the third error. The default policy\n // is to retry for several minutes.\n auto client =\n gcs::Client(google::cloud::Options{}.set\u003cgcs::RetryPolicyOption\u003e(\n gcs::LimitedErrorCountRetryPolicy(3).clone()));\n\n StatusOr\u003cgcs::ObjectMetadata\u003e object_metadata =\n client.InsertObject(bucket_name, object_name, std::move(contents),\n gcs::IfGenerationMatch(0));\n if (!object_metadata) throw std::move(object_metadata).status();\n\n std::cout \u003c\u003c \"The object \" \u003c\u003c object_metadata-\u003ename()\n \u003c\u003c \" was created in bucket \" \u003c\u003c object_metadata-\u003ebucket()\n \u003c\u003c \"\\nFull metadata: \" \u003c\u003c *object_metadata \u003c\u003c \"\\n\";\n }\n\nThe default policies are to continue retrying for up to 15 minutes, and to use truncated (at 5 minutes) exponential backoff, doubling the maximum backoff period between retries.\n\nBy default the library retries all operations, even those that are not idempotent. Application developers can override the [idempotency policy](/cpp/docs/reference/storage/2.20.0/classgoogle_1_1cloud_1_1storage_1_1IdempotencyPolicy) to follow a more conservative approach: \n\n namespace gcs = ::google::cloud::storage;\n using ::google::cloud::StatusOr;\n [](std::string const& bucket_name, std::string const& object_name,\n std::string const& contents) {\n // Create a client that only retries idempotent operations, the default is\n // to retry all operations.\n auto client =\n gcs::Client(google::cloud::Options{}.set\u003cgcs::IdempotencyPolicyOption\u003e(\n gcs::StrictIdempotencyPolicy().clone()));\n StatusOr\u003cgcs::ObjectMetadata\u003e object_metadata =\n client.InsertObject(bucket_name, object_name, std::move(contents),\n gcs::IfGenerationMatch(0));\n if (!object_metadata) throw std::move(object_metadata).status();\n\n std::cout \u003c\u003c \"The object \" \u003c\u003c object_metadata-\u003ename()\n \u003c\u003c \" was created in bucket \" \u003c\u003c object_metadata-\u003ebucket()\n \u003c\u003c \"\\nFull metadata: \" \u003c\u003c *object_metadata \u003c\u003c \"\\n\";\n }\n\n###### See Also\n\n[LimitedTimeRetryPolicy](/cpp/docs/reference/storage/2.20.0/classgoogle_1_1cloud_1_1storage_1_1LimitedTimeRetryPolicy) and [LimitedErrorCountRetryPolicy](/cpp/docs/reference/storage/2.20.0/classgoogle_1_1cloud_1_1storage_1_1LimitedErrorCountRetryPolicy) for alternative retry policies.\n\n###### See Also\n\n[ExponentialBackoffPolicy](/cpp/docs/reference/storage/2.20.0/namespacegoogle_1_1cloud_1_1storage) to configure different parameters for the exponential backoff policy.\n\n###### See Also\n\n[AlwaysRetryIdempotencyPolicy](/cpp/docs/reference/storage/2.20.0/classgoogle_1_1cloud_1_1storage_1_1AlwaysRetryIdempotencyPolicy) and [StrictIdempotencyPolicy](/cpp/docs/reference/storage/2.20.0/classgoogle_1_1cloud_1_1storage_1_1StrictIdempotencyPolicy) for alternative idempotency policies."]]