This page describes how to enable, disable, and check the status of Object Versioning on a bucket. See Using versioned objects to learn how to list, restore, and delete the objects that are retained by Object Versioning.
Required roles
To get the permissions that you need to set and manage Object Versioning
on a bucket, ask your administrator to grant you the Storage Admin
(roles/storage.admin
) IAM role on the bucket or the project
that contains the bucket. This predefined role contains the permissions
required to set and manage Object Versioning for a bucket. To see the exact
permissions that are required, expand the Required permissions section:
Required permissions
storage.buckets.get
storage.buckets.update
storage.buckets.list
- This permission is only required if you plan on using the Google Cloud console to perform the instructions on this page.
You might also be able to get these permissions with custom roles.
For information about granting roles on buckets, see Use IAM with buckets. For information about granting roles on projects, see Manage access to projects.
Set Object Versioning on a bucket
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, click the name of the bucket for which you want to enable or disable Object Versioning.
Select the Protection tab near the top of the page.
The current status of Object versioning is found in the Object versioning section.
In the Object versioning section, click the current status to make changes to it.
The Object versioning dialog appears.
- If you're enabling Object Versioning and you want to minimize storage costs, select the Add recommended lifecycle rules to manage version costs checkbox.
Click Confirm.
Command line
Use the gcloud storage buckets update
command with the
appropriate flag:
gcloud storage buckets update gs://BUCKET_NAME FLAG
Where:
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.FLAG
is either--versioning
to enable Object Versioning or--no-versioning
to disable it.
If successful, the response looks similar to the following example:
Updating gs://my-bucket/... Completed 1
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for client libraries.
The following sample enables Object Versioning on a bucket: The following sample disables Object Versioning on a bucket:C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Create a JSON file that contains the following information:
{ "versioning": { "enabled": STATE } }
Where STATE is either
true
orfalse
.Use
cURL
to call the JSON API with aPATCH
Bucket request:curl -X PATCH --data-binary @JSON_FILE_NAME \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=versioning"
Where:
JSON_FILE_NAME
is the path for the JSON file that you created in Step 2.BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Create an XML file that contains the following information:
<VersioningConfiguration> <Status>STATE</Status> </VersioningConfiguration>
Where STATE is either
Enabled
orSuspended
.Use
cURL
to call the XML API, with aPUT
Bucket request andversioning
query string parameter:curl -X PUT --data-binary @XML_FILE_NAME \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/BUCKET_NAME?versioning"
Where:
XML_FILE_NAME
is the path for the XML file that you created in Step 2.BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.
Once Object Versioning is enabled, each time a live object version is replaced or deleted, that version becomes a noncurrent version.
Check whether Object Versioning is enabled
To check whether Object Versioning is enabled on a bucket:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, the Object Versioning status of each bucket is found in the Protection column.
If enabled, the text Object versioning appears.
Command line
Use the gcloud storage buckets describe
command with the
--format
flag:
gcloud storage buckets describe gs://BUCKET_NAME --format="default(versioning)"
Where BUCKET_NAME
is the name of the bucket
whose status you want to view. For example, my-bucket
.
If successful and Object Versioning is enabled, the response looks similar to the following example:
versioning: enabled: true
If successful and Object Versioning is not enabled, the response looks similar to the following example:
null
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aGET
Bucket request:curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=versioning"
Where
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.
If successful and Object Versioning is enabled, the response looks similar to the following example:
{ "versioning": { "enabled": true } }
If successful and Object Versioning is not enabled, the response looks similar to the following example:
{}
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API, with aGET
Bucket request andversioning
query string parameter:curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/BUCKET_NAME?versioning"
Where
BUCKET_NAME
is the name of the relevant bucket. For example,my-bucket
.
If successful and Object Versioning is enabled, the response looks similar to the following example:
<VersioningConfiguration>
<Status>Enabled</Status>
</VersioningConfiguration>
If successful and Object Versioning is not enabled, the response looks similar to the following example:
<VersioningConfiguration/>
What's next
- Learn more about Object Versioning.
- Learn how to work with noncurrent objects.
- Learn how to use Object Lifecycle Management to automatically manage object versions.
- Learn how to use request preconditions to prevent race conditions.