Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Versioning

You should use versioning to keep a complete record of your objects over time. It also acts as a protection mechanism against accidental deletion of your objects. When you request a versioned object, Boto3 will retrieve the latest version.

When you add a new version of an object, the storage that object takes in total is the sum of the size of its versions. So if you’re storing an object of 1 GB, and you create 10 versions, then you have to pay for 10GB of storage.

00:00 Versioning is important to keep a record of how your objects change over time. If you accidentally modify or delete an object, it also gives you a way to revert those changes as needed.

00:12 If you request an object with boto3, it will default to grab the latest version available. Keep in mind that you need to pay to keep each version of an object that you store on S3.

00:24 If you have 10 versions of a one-gigabyte object, you need to pay for 10 gigabytes of storage. Let’s start off by defining a function to enable versioning for a bucket, and then set the first bucket to enable versioning.

00:38 Go ahead and define enable_bucket_versioning(). This is going to take an s3_connection and a bucket_name.

00:54 So, make a new variable called bkt_versioning (bucket versioning) and set this equal to your s3_connection, and then the BucketVersioning() class, which you’ll pass in the bucket_name. From here, take that bkt_versioning and call .enable().

01:20 And then for us, you can print out the bkt_versioning.status. Okay! Let’s open up a terminal and see how this works. I’m going to start python, import boto3, and then from boto3_guide import enable_bucket_versioning.

01:49 To get this started, we need to define the first_bucket_name again, which I’ve got saved here.

02:00 I’m going to copy that, paste that in, and I’m going to make an S3 resource interface, set that equal to boto3.resource('s3'). All right! Now it’s time to enable bucket versioning using that s3_resource and the first_bucket_name.

02:33 All right. And I didn’t see any errors, but it did print out None which implies that this bkt_versioning.status property didn’t return anything.

02:44 And sometimes that happens. So, if I go over to my S3 buckets and check out the firstpythonbucket that I’ve got here and go to Properties, I can see that Versioning here is Enabled.

03:01 So this is something that’s always worth checking while you’re working on, you know, coming up with a Python workflow for S3—you can always go to the S3 console and verify everything that you think you’re doing. If we hop back over, let’s add some new versions to the first file in this bucket.

03:22 To get started, I’m just going to make first_file_name and set this equal to the first filename that’s here. Paste that in. And then also the third_file_name, and let’s set this equal to this guy.

03:46 So, if we were to take this s3_resource and make an Object here using the first_bucket_name and passing in the first_file_name, we could go ahead and upload a file that is the first_file_name.

04:05 So this is basically uploading the same file over again. And from here, if you wanted to check the ID of that object now,

04:17 you can just define it again using the first_file_name and grab the .version_id property. And that gives us something here, it’s like 'G9Ky' something.

04:31 So now let’s take that interface again, make a new Object using the first_bucket_name and the first_file_name.

04:49 And upload a file, except this time you’re going to pass in the third_file_name. And keep in mind, when you’re defining these objects, this filename is referring to the key, and the key is always how Amazon S3 is referring to your files. Once you get into actual file names, like the parameters for .upload_file() or .download_file(), these file names are now referring to what’s on your computer. So in my case, this is my third file over here.

05:20 So let’s run that, and it didn’t get any errors. And to check that ID again, let’s go up and pull the .version_id for the first bucket and that first file key.

05:34 And you can see now that it’s like 'jSqm', so it’s changed. And that’s all there is to it! You don’t have to worry about getting the latest version of a file with Boto3, but you can rest easy knowing that your S3 bucket is now storing all versions of the contents within. In the next video, you’re going to learn how to traverse across your buckets and objects so that you can either get info or apply actions to all of them. Thanks for watching.

Become a Member to join the conversation.