AWS S3 Versioning

Learn via video courses

Overview

AWS offers the AWS S3 versioning as a key feature of the S3 bucket which helps the users to keep multiple copies of an object in the Amazon S3 bucket. These objects can then enable easy collaboration based on defined files that can be accessed using their respective versions, or even the changes made over time can be saved in the current state for the latest release. Objects don't get permanently deleted but are marked as deleted as the current version. It's easier and more convenient to restore any of the previous versions.

Introduction to S3 Versioning

When working with objects in the S3 bucket there might arise scenarios where due to some unseen accidents or any application failure the objects inside the S3 bucket might get lost. Recovering these objects might cost time and effort and still, not all objects may be recovered. To resolve this issue, AWS offers the AWS S3 versioning as a key feature of the S3 bucket which helps the users to keep multiple copies of an object in the Amazon S3 bucket. These objects can then enable easy collaboration based on defined files that can be accessed using their respective versions, or even the changes made over time can be saved in the current state for the latest release.

By implementing the S3 buckets by enabling the S3 versioning, you can recover from accidental actions as listed below:

  • Objects don't get permanently deleted but are marked as deleted as the current version.
  • It is easier and more convenient to restore any of the previous versions.
  • The new version of an S3 object gets created when you overwrite an existing one.

The below diagram is how the AWS S3 versioning happens for the object in that S3 bucket:

Introduction to S3 Versioning

What are the Objects and Buckets in AWS Versioning?

We have been hearing a lot about objects and buckets in AWS versioning, let us understand them in detail. We define an S3 bucket as a simple storage container for objects. Now the object is described as any file as well as the data that defines or describes it, like name, location, or size. For string the objects in S3 you will always have to create the S3 bucket to upload the files directly into it like we upload the files/ text in the folders on the local machine. Once they are uploaded successfully, based on requirements we can download and move them around without any issues arising such as loading times like with any traditional web servers.

Significance of AWS Versioning

Let us, deep dive, into understanding the significance associated with AWS S3 versioning and why it is important. We refer to ‘Versioning’ as the process of maintaining different variants of an object in the same AWS S3 bucket. We then utilize it for retrieving, preserving, and restoring the version as per our requirements. Versioning provides easy and effective maintenance of a single current object version as well as zero or more non-current object versions. It finds its main use case when it comes to easily recovering from unintended user actions(accidentally deleted or have been overwritten) and application failure. Multiple objects can be stored in an S3 bucket having the same key, but it's important to note that the IDs would be of different versions.

The versioning can be validated as the AWS S3 bucket is found to be present in one of the below given three states:

  • Unversioned state (Default state)
  • Version-enabled state
  • Version suspended state

Note: The S3 bucket can never go back to an unversioned state, once the versioning is enabled on it. While if you want you can still move the same S3 bucket to the suspended version state.

How S3 Versioning Works?

Below we have listed a few important key takeaways, which explain how S3 versioning works in various scenarios:

  • When you delete an object(not removing permanently), the AWS S3 inserts a delete marker into it, that states its current version, and when the requirement arises the previous versions can be restored.
  • When the first-time versioning is enabled, all the objects in the bucket from then on remain versioned with a new and unique version ID.
  • For objects that are overwritten, the object then has new data, which is considered a new version of that object. Here also the previous versions of the object can be restored.
  • Users currently have an object with the expiration lifecycle policy in the non-version enabled S3 bucket, and once they enable the S3 versioning if they want to maintain the same permanent delete behavior, it can be done by explicitly adding a noncurrent expiration policy. This policy shall help to manage the deletes.
  • All the objects inside the S3 buckets are subjected to S3 versioning once it is enabled in that S3 bucket.
  • All the objects stored in the S3 bucket before version enabling contain a null version ID. The existing objects don’t change when the S3 versioning is enabled.
  • The S3 bucket can only be suspended ( if the requirement arises) by either the owner or user having the appropriate permissions to access the S3 bucket to stop accruing the object versions.

Every S3 bucket in the AWS account has an S3 versioning subresource associated with it. At default, the S3 bucket is disabled with S3 versioning, along with the versioning subresource stored on the empty S3 versioning configuration, as below.

For enabling the S3 versioning, we can send a request to AWS S3 with an S3 versioning configuration having the Enabled status set ON.

For suspending the S3 versioning, you can simply set the status value to ‘Suspended’.

How to Check If Versioning is Enabled on an S3 Bucket?

While we talk about S3 Versioning and its significance, its important to understand the basic steps to follow for validating if the S3 versioning is enabled on the S3 bucket or not:

  • login to the AWS Console via the link for AWS S3
  • Choose your S3 bucket for which you need to validate the S3 versioning in AWS.
  • Navigate to the Properties tab.
  • You shall find the status which would reflect if the S3 Versioning is enabled or not from the Versioning section.

How to Enable Versioning?

Let us quickly deep dive learning how can we enable the AWS S3 versioning on the S3 bucket in the AWS account. By enabling the S3 versioning we keep track and record all the activity which includes uploads and modification of files that have taken place on the S3 bucket, keeping the multiple versions of the S3 object in the S3 bucket.

You can enable the S3 version on the S3 bucket in the AWS account, via any of the ways, that is, using the REST API, AWS Management Console, AWS SDKs, and AWS Command Line Interface (AWS CLI).

Note: You might see a delay when you enable the S3 versioning on the S3 bucket for the first time, to fully propagate the changes. It is recommended to wait for 15 minutes after enabling the S3 versioning before you issue any write operations (PUT or DELETE) on the S3 objects in the same S3 bucket.

Every S3 bucket in the AWS account has an S3 versioning subresource associated with it. At default, the S3 bucket is disabled with S3 versioning, along with the versioning subresource stored on the empty S3 versioning configuration, as below.

For enabling the S3 versioning, we can send a request to AWS S3 with an S3 versioning configuration having the Enabled status set ON.

For suspending the S3 versioning, you can simply set the status value to ‘Suspended’.

Also, to enable the S3 versioning, either the S3 bucket owner or the authorized IAM users can enable the S3 versioning. The S3 bucket owner is the AWS account holder(the root account) who created the S3 bucket. The below commands can be utilized for enabling the S3 versioning via the AWS CLI

The below commands can be utilized for enabling the S3 Versioning and multi-factor authentication (MFA) deletion on an S3 bucket.

For enabling the versioning via the AWS SDKs, the below python code can be used.

For enabling the S3 versioning via the AWS console,Manage-Versioning please refer to the Using the S3 console link.

Configuring Versioning on an S3 Bucket

Now we shall be deep diving into learn configuring the S3 versioning into your S3 bucket in the AWS account:

  • login to the AWS Management Console and search S3 in the search box where you shall see the standard AWS S3 dashboard, start the creation of the S3 bucket by selecting ‘create buckets’ as shown below.

Configuring Versioning on an S3 Bucket-1

  • Now move to select the ‘Region’ that is near to you, and specify the name (unique name to be allowed else the bucket doesn't get created) to your AWS S3 bucket. You can also select the ‘Create Bucket’ button, leave all the settings as default and scroll down as shown below.

Configuring Versioning on an S3 Bucket-2

  • Now that we created our first S3 bucket, select this S3 bucket name to open it. As shown below, we are now heading to enable the versioning on these buckets.

Configuring Versioning on an S3 Bucket-3

  • You may find a list of various features offered in the S3 buckets like Metrics, Objects, Properties, Permissions, and others. Upload any file/ picture as the first object into the S3 bucket using the Upload option, we uploaded the JPG pic as shown below.

Configuring Versioning on an S3 Bucket-4

  • As shown below, select the Add files option to choose this image in the .jpg format. Configuring Versioning on an S3 Bucket-5

  • You can also leave the rest of the settings as default, and upload the image in the S3 bucket. Once uploaded you can 'Open' the image to double-check if it is properly updated as shown below.

Configuring Versioning on an S3 Bucket-6

  • You also get to view the S3 bucket’s properties by selecting the option shown below.

Configuring Versioning on an S3 Bucket-8

  • Select the Edit button under the S3 Bucket Versioning as shown in the screenshot below from the menu, once you clicked it the S3 versioning is enabled in your S3 bucket. Don't forget to Save the changes.

Configuring Versioning on an S3 Bucket-9

  • In the screenshot below, similarly, you shall also find a new option called ‘Show versions’ that can also be enabled.

Configuring Versioning on an S3 Bucket-10

  • Once enabled, you will see a new column named Version ID that displays the S3 object name. As the image (JPG image) was the first object, the version id will be null, This indicates that this file was uploaded to the S3 bucket before versioning was enabled.
  • You can similarly upload more files and you will see that for each file upload a new and unique version id is getting allocated to it which can see from the ‘Show versions’ on the bucket page as shown below.

Configuring Versioning on an S3 Bucket-12

  • Let us try to delete the previously uploaded image file along with disabling the ‘Show Versions’ option. For this, select any file and select the ‘Delete Objects’ option… You also see a popup for the warning message will be thrown as shown below.

  • That file gets deleted permanently by typing the permanently as seen in the below image.

Configuring Versioning on an S3 Bucket-13

  • You also get the option to permanently delete any delete marker file by easily selecting that specified file as the delete marker. Then select the ‘Delete objects’ and that file is permanently deleted.

  • Once you go back to the S3 bucket page and enable the Show version option, you shall see that the files are deleted.

Hence, after the exercise, we know that with S3 versioning you can stay protected against any accidental deletes.

Using Versioning in S3 Buckets

To preserve, retrieve, and restore each version of all the objects residing in the S3 buckets, Versioning helps to do so enabling easy recovery from both unintended user actions as well as application failures. Once the S3 versioning is enabled for an S3 bucket, the S3 shall receive various write requests for the same object simultaneously, it then stores all the versions of those S3 objects. With versioning enabled you can also go back and restore the previous version. It is recommended to use the S3 version with the S3 lifecycle for customizing the data retention approach and controlling the storage costs.

S3 Versioning is disabled on the S3 buckets by default and it's necessary to explicitly enable the S3 version on the S3 bucket.

You may find your S3 Buckets can be in one of three states:

  • Unversioned (By default)
  • Versioning-enabled
  • Versioning-suspended

Example of AWS S3 Versioning

With this section, we shall be learning about two major topics of S3 versioning, that is,

  • S3 Version IDs
  • S3 Versioning workflows

S3 Version IDs:

When the S3 versioning is enabled for the S3 bucket, then a unique version ID is generated for the object that is being stored. As seen in the below diagram, you can see how S3 Versioning works for an S3 versioning-enabled bucket containing two objects with the same key but different version IDs like version 111111 and version 121212.

Example-1

The version Id is null for the objects that are in the S3 bucket before the versioning was enabled or the first time when the S3 versioning was enabled where this version ID helps to distinguish between objects from other versions of the same key.

It is important to highlight that only AWS S3 generates the version IDs, which cannot be edited. The Version IDs are UTF-8 encoded, Unicode, opaque strings, URL-ready that are no more than 1,024 bytes long as shown below,

8sL4kqtJlcpXroD76Hb+rmSpXd3dIbrHY+MTgtHbsvjVBH40Nr8X8gdRQBpUMLUo

S3 Versioning workflows:

Now let us highlight the S3 versioning workflows.

Case1: When an object is 'PUT' in an S3 versioning-enabled bucket, the noncurrent version does not get overwritten. As seen below, when a new object is 'PUT' into the S3 bucket containing an object with the same name, the below-shown behavior occurs:

Example-2

With this, you can easily retrieve a previous version of an object after any accidental delete or overwrite

Case2: When an S3 object is pushed for a 'DELETE' operation, all the existing versions remain as-is in the S3 bucket, while the AWS S3 inserts a delete marker for that object, as shown below. Also, now the delete marker becomes the current version of the object.

Example-3

Case3: When you receive a 'GET' request, by default, the most recently stored version is sent in response. When the current version is a delete marker, and you receive a GET request, a 404 Not Found error, as shown below.

Example-4

Case4: Permanently deleting the object is also possible from an S3 object by specifying the version of that object without inserting a delete marker. For doing so, only the owner of the AWS S3 bucket is permitted.

Example-5

Case5: More security can be configured a S3 bucket to enable multi-factor authentication (MFA) delete.

Disadvantages of Enabling the Versioning of a Bucket

While we talk about the S3 versioning and its various benefits and features that it offers to the users, let us briefly discuss a few of the cons of enabling the versioning of an S3 bucket in your AWS account.

  • With the S3 version, multiple versions of an object(files) are created, which leads to an increase in the size of the S3 bucket. Like the same file if uploaded multiple times. due to the production cycle (regression), will convert to two times the size of the same file that is, a 2GB file if uploaded thrice would take away 6GB of space as S3 takes both files with different versions if the version is filed in the S3 bucket.
  • With that being said, as we know we pay for what we use, hence the same 2GB file which has been versioned thrice, would be costed at thrice the price for the same file in TimedStorage-ByteHrs.

Conclusion

  • The S3 bucket can never go back to an unversioned state, once the versioning is enabled on it. While if you want you can still move the same S3 bucket to the suspended version state.
  • We can check the multiple versions of a file in S3 via the list-version toggle through the AWS S3 console inside the S3 bucket.
  • With the S3 version, multiple versions of an object(files) are created, which leads to an increase in the size of the S3 bucket. Like the same file if uploaded multiple times. due to the production cycle (regression), will convert to two times the size of the same file.
  • You might see a delay when you enable the S3 versioning on the S3 bucket for the first time, to fully propagate the changes. It is recommended to wait for 15 minutes after enabling the S3 versioning before you issue any write operations (PUT or DELETE) on the S3 objects in the same S3 bucket.
  • When an S3 object is pushed for a 'DELETE' operation, all the existing versions remain as-is in the S3 bucket, while the AWS S3 inserts a delete marker for that object, as shown below. Also, now the delete marker becomes the current version of the object.