To support the retrieval of objects that are deleted or overwritten, Cloud Storage offers the Object Versioning feature. This page describes the feature and the options available when using it. To learn how to enable and use Object Versioning see Using Object Versioning.
Enable Object Versioning to protect your Cloud Storage data from being overwritten or accidentally deleted. Enabling Object Versioning increases storage costs, which can be partially mitigated by configuring Object Lifecycle Management to delete older object versions.
You enable Object Versioning for a bucket. Once enabled:
Cloud Storage creates a noncurrent version of an object each time you perform an overwrite or delete of the live version, as long as you do not specify the generation number of the live version.
Noncurrent versions retain the name of the object, but are uniquely identified by their generation number.
Noncurrent versions only appear in requests that explicitly call for object versions to be included.
You permanently delete versions of objects by including the generation number in the deletion request or by using Object Lifecycle Management.
Noncurrent versions of objects exist independently of any live version.
You can turn versioning on or off for a bucket at any time. Turning off versioning leaves existing object versions in place and causes the bucket to stop accumulating new noncurrent versions of objects.
Object Versioning details
Cloud Storage uses two properties that together identify the version of an object. One property identifies the version of the object's data; the other property identifies the version of the object's metadata. These properties are always present with every version of the object, even if Object Versioning is not enabled. These properties can be used as preconditions for conditional updates to enforce ordering of updates.
Cloud Storage marks every object using the following properties:
||Identifies the content (data) generation, and updates when the content of an object is overwritten. There is no relationship between the generation numbers of unrelated objects, even if the objects are in the same bucket.|
||Identifies the metadata generation, and increases every time the metadata for a given content generation is updated.
Object Versioning cannot be enabled on a bucket that currently has a retention policy.
Noncurrent object metadata
Noncurrent versions of objects have their own metadata, which may differ from the metadata of the live version. Most importantly, a noncurrent version retains its ACLs and does not necessarily have the same permissions as the live version.
Each version, whether live or noncurrent, has one set of metadata; only the latest metageneration number refers to metadata. Older metageneration numbers cannot be used to access metadata that has since been changed.
You can update metadata for a noncurrent version of an object by specifying its
generation in your request. To ensure safe read-modify-write semantics, you
can use a metageneration-match precondition. Using this precondition causes
the update to fail if the metadata you are attempting to update was
changed between the time you read the metadata and sent the update.
Object Versioning example
This example shows what happens to the file
cat.jpg in a bucket
with Object Versioning enabled as you overwrite, update, and delete the file.
- You upload a new image
When you first upload
cat.jpgto Cloud Storage, it receives a
generationnumber and a
metagenerationnumber. In this example, the generation number is
1360887697105000. Because the object is new, the
metagenerationnumbers even though Object Versioning is not enabled. You can view these numbers by using the
statcommand in gsutil. For instructions, see viewing the object metadata.
- You enable Object Versioning
At this point, you decide to enable Object Versioning for your bucket. Doing so does not affect the
- You change the metadata of the image
You update the metadata for
cat.jpgby adding custom metadata:
color:black. Updating metadata causes the
cat.jpgto increase, in this case from
2. However, the object itself remains unchanged, so Cloud Storage continues to store only one version of
cat.jpg, and the version continues to have a
- You upload a new version of the image
You upload a new version of
cat.jpgto your Cloud Storage bucket. When you do so, Object Versioning moves the existing
cat.jpgobject into a noncurrent state. The noncurrent version retains the same storage class and metadata it previously had. The noncurrent version appears only if you perform a versioned listing: it does not appear in normal listing commands. The noncurrent version is now referenced as:
Meanwhile, the newly uploaded
cat.jpgbecomes the live version of the object. This new
cat.jpggets its own
generationnumber, in this example
1360887759327000. It also gets its own metadata and a
1, which means it does not contain the
color:blackmetadata unless you specify it. When you access or modify
cat.jpg,this is the version that is used. You can alternatively refer to this version of
generationnumber. For example, when using the gsutil tool you would refer to it as
- You delete the live version of the image
You now delete
cat.jpg. When you do this, the version that had generation number
1360887759327000becomes noncurrent. Your bucket now contains two noncurrent versions of
cat.jpgand no live versions. You can still refer to either noncurrent version by using its
generationnumber, but if you try to access
generationnumber, it fails.
Similarly, a normal object listing of the bucket will not show
cat.jpgas one of the objects in the bucket. For information on listing noncurrent versions of objects, see Listing noncurrent object versions.
- You disable Object Versioning
You disable Object Versioning, which stops objects from becoming noncurrent. Existing noncurrent versions of objects remain in Cloud Storage. Even though Object Versioning is disabled,
cat.jpg#1360887759327000remain stored in your bucket until you delete them, either manually or through using Object Lifecycle Management.
- You restore one of the noncurrent versions
Even with Object Versioning disabled, you can restore one of the existing noncurrent versions by making a copy of it. To do so, simply name the copy you make
cat.jpg. Once you do this, your bucket has three versions of
cat.jpg: the two noncurrent versions and the live version that came from making a copy.
This section discusses tips to help you work with Object Versioning more effectively.
- The gsutil tool has comprehensive support for working with versioned objects
that makes many tasks involving Object Versioning easier. For example, you
can work with noncurrent versions of objects by appending
generationnumber to the object name. For more information on using gsutil with Object Versioning, see Object Versioning and Concurrency Control.
- Consider using
metagenerationnumbers for conditional updates instead of ETags. Together,
metagenerationnumbers keep track of all object updates, including metadata changes, providing a stronger guarantee than ETags.
Understand file restoration behavior
You can copy a noncurrent object version to the current live version. See Copying noncurrent object versions for a step-by-step guide to copying noncurrent versions of objects.
When you do this with Object Versioning enabled, if there already exists a live version of the object in your bucket, Cloud Storage overwrites it but also creates a new, noncurrent version of the overwritten object. In such a case, your bucket subsequently contains the overwritten object (now noncurrent) and two copies of the object that was previously noncurrent (one live copy and one still-noncurrent copy), all of which incur storage charges. To prevent unnecessary charges, delete the noncurrent version that you used to make the current live copy.
Use generation-match preconditions when mutating live versions of objects
When you use generation numbers, a request succeeds as long as there is an object with that name and generation number, regardless of whether it is live or noncurrent. If no such object exists, Cloud Storage returns
404 Not Found.
When you use generation-match preconditions, a request succeeds only if the live version of the requested object has the specified generation number. If no such object exists, or is noncurrent, Cloud Storage returns
412 Precondition Failed.
You should avoid using a generation-match precondition at the same time as a generation number in the object name. If you use both and the numbers match, the use of the precondition is redundant. If the numbers do not match, the request always fails.
If you make several concurrent mutation requests with a generation-match precondition, Cloud Storage's strong consistency allows only one of those requests to succeed. This feature is useful if your objects are updated from several sources and you need to ensure that users don't accidentally overwrite them.
If you set a generation-match precondition to
0when uploading an object, Cloud Storage performs the specified request only if there is no live version of the object. For example, if you perform a
PUTrequest with the XML API to create a new object with the header
x-goog-if-generation-match:0, the request succeeds if the object does not exist, or if there are only noncurrent versions of the object. If there is a live version of the object, Cloud Storage aborts the update with a status code of
412 Precondition Failed.