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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Moving and Deleting Objects

Give Feedback

Now that you can upload and download files to your buckets, you may run into a case where you need to copy your objects between buckets. You might be migrating items from an older bucket to a newer bucket, or you might be backing something up. Either way, it’s a relatively straightforward process using the resource interface.

00:00 Now that you can upload and download files to your buckets, you may run into a case where you need to copy your objects between buckets. You might be migrating items from an older bucket to a newer bucket, or you may be backing something up. Either way, it’s a relatively straightforward process using the resource interface.

00:18 Go ahead and define a new function called copy_to_bucket(). You can pass your s3_connection, the from_bucket, to_bucket and the file_name.

00:37 Make a new dictionary called copy_source

00:43 and set this equal to a 'Bucket', which is going to be the from_bucket, and a 'Key', which is the file_name. And once you have that, you can take your s3_connection, call an .Object() off of that, and this new Object is going to be in the new bucket and it will have the same filename, and then you’ll just copy from the copy_source.

01:17 So if you look at this, what you’re really doing is creating a new object that’s in the new bucket, and then copying the file from the old object into the new one.

01:27 All right, open up a terminal and we can try this out. I’m going to open up Python, first import boto3, and then from boto3_guide, I’m going to import copy_to_bucket. And here I’m going to need some names, so first_bucket_name, set that equal to

01:57 the first bucket. And, of course, I copied too much and got the newline character, so let’s just try that again.

02:09 And this time, just the name. I’m going to need the second_bucket_name,

02:28 the file_name,

02:37 and I’ll make a new resource interface just called s3_resource, set that equal to boto3.resource('s3'). And if all that’s correct, you should be able to call copy_to_bucket(), and you’re going to pass in that s3_resource, the first_bucket_name, the second_bucket_name,

03:10 and the file_name. All right, let’s run that and see what happens. All right! No errors, so that copied successfully. Now that you’ve copied that file to the second bucket, let’s go ahead and delete it from the first bucket.

03:24 You’re going to go ahead and create a new object from the resource interface, which contains—if you may have guessed—a .delete() method. So, from this interface, go s3_resource, define a new Object, and let’s go ahead and put the first_bucket_name in there and the file_name. And now that you’ve defined this Object instance, you can just call .delete(), just like that.

03:56 Now if you look, this gave a little bit more of a readout, and if you look in here, there’s a number of things relating to that .delete() operation, but the big thing you can take away is that you got a 204 response code, which just means that this was a successful request and there’s nothing more for the server to send to us. Which makes sense, because you just deleted something. All right!

04:20 That pretty much wraps up the core functionality of working with S3 and Boto3, as you can now create buckets, upload files to them, move those files, download them, and delete them. In the next few videos, we’re going to take a look at some advanced S3 features and talk about how they can help your project out. Thanks for watching.

Become a Member to join the conversation.