Conditional Requests Via ETag / Generation / Metageneration Preconditions

Preconditions tell Cloud Storage to only perform a request if theETag, generation, ormetageneration number of the affected objectmeets your precondition criteria. These checks of the ETag, generation, andmetageneration numbers ensure that the object is in the expected state,allowing you to perform safe read-modify-write updates and conditionaloperations on objects

Concepts

ETag

An ETag is returned as part of the response header whenever a resource isreturned, as well as included in the resource itself. Users should make noassumptions about the value used in an ETag except that it changes whenever theunderlying data changes, per thespecification

TheETag attribute is set by the GCS back-end, and is read-only in theclient library.

Metageneration

When you create aBucket,itsmetageneration is initializedto1, representing the initial version of the bucket’s metadata.

When you first upload aBlob (“Object” in the GCS back-end docs),itsmetageneration is likewiseinitialized to1. representing the initial version of the blob’s metadata.

Themetageneration attribute is set by the GCS back-end, and is read-onlyin the client library.

Each time you patch or update the bucket’s / blob’s metadata, itsmetageneration is incremented.

Generation

Each time you upload a new version of a file to aBlob (“Object” in the GCS back-end docs),the Blob’sgeneration is changed, and itsmetageneration is reset to1 (the firstmetadata version for that generation of the blob).

Thegeneration attribute is set by the GCS back-end, and is read-onlyin the client library.

See also

Conditional Parameters

Usingif_etag_match

Passing theif_etag_match parameter to a method which retrieves ablob resource (e.g.,Blob.reload)makes the operation conditional on whether the blob’s currentETag matchesthe given value. This parameter is not supported for modification (e.g.,Blob.update).

Usingif_etag_not_match

Passing theif_etag_not_match parameter to a method which retrieves ablob resource (e.g.,Blob.reload)makes the operation conditional on whether the blob’s currentETag matchesthe given value. This parameter is not supported for modification (e.g.,Blob.update).

Usingif_generation_match

Passing theif_generation_match parameter to a method which retrieves ablob resource (e.g.,Blob.reload) or modifiesthe blob (e.g.,Blob.update)makes the operation conditional on whether the blob’s currentgenerationmatches the given value.

As a special case, passing0 as the value forif_generation_matchmakes the operation succeed only if there are no live versions of the blob.

Usingif_generation_not_match

Passing theif_generation_not_match parameter to a method which retrievesa blob resource (e.g.,Blob.reload) or modifiesthe blob (e.g.,Blob.update)makes the operation conditional on whether the blob’s currentgenerationdoesnot match the given value.

If no live version of the blob exists, the precondition fails.

As a special case, passing0 as the value forif_generation_not_matchmakes the operation succeed only if thereis a live version of the blob.

Usingif_metageneration_match

Passing theif_metageneration_match parameter to a method which retrievesa blob or bucket resource(e.g.,Blob.reload,Bucket.reload)or modifies the blob or bucket (e.g.,Blob.updateBucket.patch)makes the operation conditional on whether the resource’s currentmetageneration matches the given value.

Usingif_metageneration_not_match

Passing theif_metageneration_not_match parameter to a method whichretrieves a blob or bucket resource(e.g.,Blob.reload,Bucket.reload)or modifies the blob or bucket (e.g.,Blob.updateBucket.patch)makes the operation conditional on whether the resource’s currentmetageneration doesnot match the given value.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-01-29 UTC.