0
0
GCPcloud~5 mins

Object versioning in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes files in cloud storage get overwritten or deleted by mistake. Object versioning helps keep old copies of files so you can recover them later.
When you want to keep backups of files automatically in cloud storage.
When multiple people edit files and you want to track changes over time.
When you want to protect important data from accidental deletion.
When you need to audit or review previous versions of files.
When you want to restore a file to an earlier state after a mistake.
Commands
This command turns on object versioning for the bucket named example-bucket. It means old versions of files will be kept when overwritten or deleted.
Terminal
gsutil versioning set on gs://example-bucket
Expected OutputExpected
Setting versioning configuration on gs://example-bucket/... Success.
Uploads a file named file.txt to the bucket. If the file already exists, the old version is saved because versioning is on.
Terminal
gsutil cp file.txt gs://example-bucket/file.txt
Expected OutputExpected
Copying file.txt [Content-Type=text/plain]... / [1 files][ 12.0 B/ 12.0 B] Operation completed over 1 objects/12.0 B.
Uploads a new version of file.txt. The previous version is kept as a noncurrent version because versioning is enabled.
Terminal
gsutil cp file_v2.txt gs://example-bucket/file.txt
Expected OutputExpected
Copying file_v2.txt [Content-Type=text/plain]... / [1 files][ 15.0 B/ 15.0 B] Operation completed over 1 objects/15.0 B.
Lists all versions of file.txt in the bucket, including old versions saved by versioning.
Terminal
gsutil ls -a gs://example-bucket/file.txt
Expected OutputExpected
gs://example-bucket/file.txt#1589281234567890 gs://example-bucket/file.txt#1589281234567891
Downloads a specific old version of file.txt by its version ID to restore or review it.
Terminal
gsutil cp gs://example-bucket/file.txt#1589281234567890 restored_file.txt
Expected OutputExpected
Copying gs://example-bucket/file.txt#1589281234567890... / [1 files][ 12.0 B/ 12.0 B] Operation completed over 1 objects/12.0 B.
Key Concept

If you remember nothing else from this pattern, remember: turning on object versioning saves old copies of files automatically so you can recover them later.

Common Mistakes
Not enabling versioning before uploading files
Old versions won't be saved if versioning is off, so you can't recover previous file states.
Always enable versioning on the bucket before uploading files you want to keep versions of.
Trying to list versions without the -a flag
The command will only show the latest version, hiding older versions.
Use 'gsutil ls -a' to see all versions of an object.
Deleting files without specifying version ID
Deletes the latest version but older versions remain; this can cause confusion if you want to fully remove a file.
Specify version ID to delete specific versions or disable versioning if you want to stop keeping old versions.
Summary
Enable object versioning on a bucket to keep old copies of files automatically.
Upload files normally; old versions are saved when files are overwritten or deleted.
Use 'gsutil ls -a' to list all versions and specify version IDs to restore or delete specific versions.