AWS

How to enable version control on an S3 bucket?

As one has to maintain multiple copies in order to have backups of data, having multiple copies without any proper metadata may lead to some disastrous situations. Previously, as we have learnt about “How To Create an S3 Bucket on AWS“, today we are going to learn about a solution (version control) for maintaining different versions of a particular type of data, further, we will be understanding about “version control on an S3 bucket”.

Step 1: Create a new S3 bucket

We are going to create an S3 bucket, as shown below we have created a bucket with a name “versioning-geekylane“.

Create a new S3 bucket versioning geekylane
Create a new S3 bucket “versioning-geekylane”

Now, after creating this bucket, we have to make the “versioning-geekylane” bucket as “public“.

If you do not know how to make an S3 bucket public, then please follow -> Make an S3 Bucket Public and look for “Step 6

Bucket now have public access
Bucket now have public access

Step 2: Enable versioning or version control on S3 bucket

From the “Amazon S3” dashboard, click on the “S3 bucket” you want to enable versioning on, after that look for “Properties” tab and from there you will be seeing something like shown below.

Under properties of an S3 bucket click on versioning
Under properties of an S3 bucket click on versioning

After that, click on the “Versioning” block and you will be asked as shown in the screenshot below.

Select “Enable versioning” as we did, and click on “Save” icon.

Click on enable versioning for the S3 bucket
Click on enable versioning for the S3 bucket

You will be seeing that now the “Versioning” is enabled.

 Versioning is now enabled for versioning geekylane S3 bucket
Versioning is now enabled for “versioning-geekylane” S3 bucket

Step 3: Upload a file to S3 bucket as “Version = 1”

In this step, we are going to create a file, here we are using “geekylane.txt” as our file holding the contents as shown below.

Note: This will be the “version 1” of this file.

Upload a geekylane txt file to versioning geekylane S3 bucket
Upload a “geekylane.txt” file to versioning geekylane S3 bucket

The file has been successfully uploaded to the S3 bucket “versioning-geekylane“.

Note: The size of the file with “version 1” is 37.0 B

Note the size and date of this file
Note the size and date of this file

Step 4: Access the file stored in S3 bucket

Note: As we have already made “versioning-geekylane” bucket public, we can directly access the contents resides under it without making any further changes to permissions.

Make sure, before accessing the file you enabled the public access to the file, otherwise, it will give you an “AccessDenied” error.

Simply copy the “Object URL” as shown below.

Copy the object URL of geekylane txt file
Copy the object URL of “geekylane.txt” file

After copying the “URL“, paste that in the “URL bar” in your browser and hit enter.

Note: The URL and file will be completely different in your case.

Access the geekylane txt file using its object url from browser
Access the “geekylane.txt” file using its object url from browser

From above, we can see that the “geeklane.txt” file stored in “versioning-geekylane” is easily accessible.

Step 5: Update the file to “Version = 2” and upload it to S3 bucket

Now, we have changed the contents of the “geekylane.txt” with the following.

The geekylane txt is updated to new contents
The “geekylane.txt” is updated to new contents

Upload the updated “geekylane.txt” file to the “versioning-geekylane” bucket, as shown in the picture below.

Note: The size of the file with “version 2” is 71.0 B

New and update geekylane txt file has been uploaded
New and update “geekylane.txt” file has been uploaded

Again, copy the “Object URL” of this updated “version 2” of “geekylane.txt” file, as shown in the image below.

Copy the URL of the updated geekylane txt file
Copy the URL of the updated “geekylane.txt” file

Paste that “URL” in the browser’s “URL bar“.

Question: Wait! what? – Why is it giving us the “AccessDenied” error?

Answer: Because we have uploaded a new version of the “geekylane.txt” file, so in order to access it, we have to again make changes to its permissions and make it public.

Unable to access the updated geekylane txt file showing access denied
Unable to access the updated “geekylane.txt” file showing access denied

Making the file public.

Make the updated geekylane txt file public
Make the updated “geekylane.txt” file public

Accessing the updated “version 2” of the “geekylane.txt” file via a web browser.

From below, you can see that now we are able to access the updated “geekylane.txt” file.

Update geekylane txt file is now publicy accessible
Update “geekylane.txt” file is now publicly accessible

Step 6: View all the versions of a file stored in an S3 bucket

Click on the toggle button “Show” in the front of the “Versions” as shown below.

After that you will be able to see all the versions of a particular file.

Click on show button toggle to view all versions of an object in S3 bucket
Click on show button toggle to view all versions of an object in S3 bucket

Click on “Hide” to only view the latest version of the file.

Click on hide again to learn about deletion of an object
Click on hide again to learn about deletion of an object

Step 7: Deleting the files stored in an S3 bucket with versioning enabled

From the picture, you can see that we are deleting the “geekylane.txt” file.

Question: Will it be going to delete all the versions of the “geekylane.txt” files?

Answer: No, it will be only going to delete the “latest version” of the file, in this case only the “version 2” of “geekylane.txt” file is going to be deleted.

Delete an object in S3 bucket with versioning on
Delete an object in S3 bucket with versioning on

After deleting the “latest version” of the file, you will be seeing something like shown below, if you do not have any other files already stored in your S3 bucket.

It will show the bucket is now empty but in reality it is not
It will show the bucket is now empty but in reality it is not

Click on the “Show” button to view the versions currently available.

Question: Why our “geekylane.txt” is not deleted?

Answer: By default, Amazon S3 will not delete the file, instead it will create a “Delete Marker” to the file.

Question: So, how do we actually delete a file stored in an S3 bucket enabled with versioning?

Answer: Simple solution is to first enable or go to the “show” all versions view so that you can see all the versions of a particular file and from there delete the version you want to be deleted.

From below, now we are in the “all version” view of a file, if we delete the “Delete Marker” as well as the “Latest Version” of the file, eventually we will be able to delete the “version 2” of our “geekylane.txt” file.

This is the benefit of versioning
This is the benefit of “versioning”

Click on the “Delete” option under “Actions” tab in order to delete the “Delete Marker” of a file.

Delete the delete marker of an object stored in S3 bucket with versioning enabled
Delete the “delete marker” of an object stored in S3 bucket with versioning enabled

Confirm the deletion of “Delete Marker“.

confirmation of deletion of delete marker
confirmation of deletion of “delete marker”

After deleting the “Delete Marker“, we are again left with the “version 1” and “version 2” of the file.

We still have our all the versions of geeklane txt file
We still have our all the versions of “geekylane.txt” file

Click on the “Hide” icon to go back to the normal view.

The geekylane txt file is restored
The “geekylane.txt” file is restored

Comment here