Cloud google cloud Object storage

Working with versioning in Google Cloud Storage

In a previous post we talked about Creating a Cloud Storage Bucket from Google console, this time we will enable versioning in a Cloud Storage bucket that has been already created. We will also cover how to archive objects, convert archived objects to a live status.

Steps

1. In the Console search bar, enter storage and click Storage in the drop-down results to navigate to the Cloud Storage section:

alt

2. Click the Activate Cloud Shell button at the top of the Google Cloud Console. We are going to run all commands from the shell and cross check using the GUI

3. Check whether Object Versioning is enabled. Run the following command:

gsutil versioning get gs://BUCKET_NAME

As shown in the image, versioning is disabled for our bucket “object-versioning”

4. To enable Object Versioning on a bucket run:

gsutil versioning set on gs://BUCKET_NAME

Run again gsutil versioning get gs://BUCKET_NAME to validate versioning is enabled:

Now let’s create a file and delete it to see how versioning works.

5. Create an empty file and upload it to our bucket

#Create Empty File in our local cloud shell

[email protected]:~ (serious-amulet-298917)$ touch testfile

#Check Directory 

[email protected]:~ (serious-amulet-298917)$ ls -l
total 4
-rw-r--r-- 1 user user 913 Dec 17 19:42 README-cloudshell.txt
-rw-r--r-- 1 user user   0 Dec 17 19:43 testfile

#Copy the file to our Bucket

[email protected]:~ (serious-amulet-298917)$ gsutil cp testfile gs://object-versioning
Copying file://testfile [Content-Type=application/octet-stream]...
/ [1 files][    0.0 B/    0.0 B]
Operation completed over 1 objects

Refresh the Web Console to see the file we just copied:

6- View the details of the bucket content by running the following command:

[email protected]:~ (serious-amulet-298917)$ gsutil ls -a gs://object-versioning
gs://object-versioning/testfile#1608234448685508

As result you will see the name of your file and a “#number’ (ie testfile#1608234448685508) append to it, which is the generation number. This generation number changes each time the object is replaced

7- Delete the file from the Web Console, so it won’t be longer available from there. But if you go back to the cloud shell and gsutil ls -a gs://object-versioning we can see the file still there, but its been archived.
To make our file live from our archive we need to run some commands

8- To make our file live from our archive we have two options. We can use the copy (cp) command to copy the file from the archive to our bucket, or move (mv) command which removes the file from the archive and moves it to our bucket. With move command we keep a single copy of the file

#Promote our archive file to live

[email protected]:~ (serious-amulet-298917)$ gsutil mv gs://object-versioning/testfile#1608234448685508 gs://object-versioning/testfile
Copying gs://object-versioning/testfile#1608234448685508 [Content-Type=application/octet-stream]...
Removing gs://object-versioning/testfile#1608234448685508...
Operation completed over 1 objects.

Refresh your web console to see the restored file. When you run ls -a gs://bucket-name you will see a new generation number generated.

[email protected]:~ (serious-amulet-298917)$ gsutil ls -a gs://object-versioning
gs://object-versioning/testfile#1608236027776707

Reference Links

https://cloud.google.com/storage/docs/generations-preconditions

https://cloud.google.com/storage/docs/object-versioning

https://cloud.google.com/storage/docs/creating-buckets