Skip to content

How to create AWS S3 versioning objects

Estimated time to read: 3 minutes

In the previews tutorial, you learned how to create a bucket using AWS S3. In this tutorial, you are creating versioning objects, and, at the same time, you will be exploring how they work.

Prerequisites

  • The steps from the previous tutorial "create AWS S3 versioning buckets".

Creation of the first object

Create a file and upload it to the S3 bucket

$ echo monkey > test.txt
$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api put-object --bucket versioned --key test.txt --body test.txt

Updating the first object

Edit the content of the file and upload it again

$ echo dolphin > test.txt
$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api put-object --bucket versioned --key test.txt --body test.txt

Edit it a second time and upload it again.

$ echo animals > test.txt
$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api put-object --bucket versioned --key test.txt --body test.txt

What did you do?

Let's sum it up;

You created a file called test.txt. In this file, you've put the text: monkey. After that, you uploaded the test.txt file to your Fuga S3 bucket.

Second, you've edited the test.txt from monkey into dolphin and uploaded the changes.

Third, you've edited the test.txt from dolphin into animals and uploaded the changes again.

How does the bucket look after these changes?

With the command list-objects-v2 it is possible to see all the objects in the Object Store.

$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api list-objects-v2 --bucket versioned
{
"Contents": [
     {
         "Key": "test.txt",
         "LastModified": "2021-11-30T09:19:42.967Z",
         "ETag": "\"566c3a260314fea8318ec46ed7efd4c5\"",
         "Size": 5,
         "StorageClass": "STANDARD"
     }
]
}

In the output above, you can only see one file. Does it mean versioning is not enabled?

Versioning is enabled, although it is not possible to see this with the command list-objects-v2. If you want to see all the versioned files, you need to use the command list-object-versions. (as shown below)

$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api list-object-versions --bucket versioned
{
"Versions": [
     {
         "ETag": "\"566c3a260314fea8318ec46ed7efd4c5\"",
         "Size": 5,
         "StorageClass": "STANDARD",
         "Key": "test.txt",
         "VersionId": "iGhlB8vxX6vhehgrdyhgo4rkBY74jvo",
         "IsLatest": true,
         "LastModified": "2021-11-30T09:19:42.967Z",
         "Owner": {
             "DisplayName": "owner name",
             "ID": "owner ID"
         }
     },
     {
         "ETag": "\"f4aa19fc7ee46cce02bf1fb6fad054cf\"",
         "Size": 5,
         "StorageClass": "STANDARD",
         "Key": "test.txt",
         "VersionId": "EGlSkj1dR.OrtmmmZ0R4DsI5aVPHNcO",
         "IsLatest": false,
         "LastModified": "2021-11-30T09:19:02.669Z",
         "Owner": {
             "DisplayName": "owner name",
             "ID": "owner ID"
         }
     },
     {
         "ETag": "\"dbb126075a1d0b4ca64c3e2ae0159bc7\"",
         "Size": 4,
         "StorageClass": "STANDARD",
         "Key": "test.txt",
         "VersionId": "swwQLkp35P87WzjHQmoYkO1KDukcvq2",
         "IsLatest": false,
         "LastModified": "2021-11-30T09:18:48.423Z",
         "Owner": {
             "DisplayName": "owner name",
             "ID": "owner ID"
         }
     }
]
}
In the example above, you see three objects present, and they all have the same Key, namely test.txt. That is why there was only one object to see with the list-objects-v2 command.

Furthermore, it’s possible to see the “VersionedId” and “IsLatest” now.

Checking out the versioned files

To check the versioning is working, you can download two objects and spot the difference when using a versionId that is not the current object.

To download the currently active object, you use the command get-object.

$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api get-object --bucket versioned --key test.txt test2.txt
$ cat test2.txt
animals

To download a versioned object, you have to select one of the versioned-ID’s provided in the list-object-versions

Note

do not take the one with IsLatest is True. It will return the same file as the one listed above it.

If you take the earliest LastModified object from the example list, you will get the file with monkey in it.

$ aws --profile=fuga --endpoint=https://core.fuga.cloud:8080 s3api get-object --bucket versioned --key test.txt --version-id swwQLkp35P87WzjHQmoYkO1KDukcvq2 test3.txt
$ cat test3.txt
monkey

Conclusion

In this tutorial, you have learned to create versioned files in a Fuga S3 bucket, how to retrieve them and find the versioned files. In the next tutorial, you'll learn how to delete a versioned object from a Fuga S3 bucket.