This week I will explain S3 versioning. Versioning basically can help us to prevent our objects. There may be problems that an object can accidentally be deleted, overwrited or expired. In that case, if we had enabled versioning, we can restore the object to it’s previous version. Versioning is enabled on the bucket ( means we can not enable versioning for specific objects ) and we can’t disable later, only we can suspend it. If you don’t want to use versioning after you enable it, you can either suspend it or create a new bucket and move your existing objects to it. And for the cost perspective, every version of objects are different objects so AWS will charge you for all of them.
Now let’s configure versioning on our bucket and see how we can use it.
First of all, I will create a bucket named “s3.version.enable”.
Then I will upload a file named “doc.txt”. A
Next I enable the versioning. When I click the “enable versioning” button, a popup will ask if I want to enable it.
When I click the “version:show” button, I can see that my object’s version is “null”. All of the versions of the objects will be “null” if you uploaded them before versioning enabled.
If I upload the file again, it will have a version id.
If you want to restore your object, you can:
– Copy a previous version of the object into the same bucket
– Permanently delete the current version of the object
Here I created a basic python script that uses the first option.
import boto key= secret= def s3_list_restore_version(): s3conn = boto.connect_s3(key,secret) source_s3_bucket=s3conn.get_bucket("s3.version.enable") for k in source_s3_bucket.get_all_versions(): print k.name,k.version_id source_s3_bucket.copy_key(k.name,source_s3_bucket.name,"doc.txt",src_version_id="GhrYXpKL0k4.pBaGDWnLl5A5GDSmt7da") if __name__ == "__main__": try: s3_list_restore_version() except: raise
As you see I want to restore the object with version id “GhrYXpKL0k4.pBaGDWnLl5A5GDSmt7da”. After the script runs , the latest object will be the the same as the restored object the same size
If you have any question or comment, please feel free to write and don’t forget to share please.