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.

Uploading and Downloading Files

Give Feedback

There are three ways you can upload a file:

  1. From an Object instance
  2. From a Bucket instance
  3. From the client

In each case, you have to provide the Filename, which is the path of the file you want to upload. To download a file from S3 locally, you’ll follow similar steps as you did when uploading. But in this case, the Filename parameter will map to your desired local path.

00:00 You’re finally ready to upload files to S3. There are three ways you can upload a file. You can do this from an Object instance, from a Bucket instance, or from the client.

00:11 We’re going to focus on the Object and Bucket instances, because these are very powerful ways of working with the AWS API. First, let’s try to upload a file using the Object instance.

00:22 Looking at the interpreter, if you still have the first_object Object, you should be able to run that and you’ll see that it has the bucket_name associated with it, and then the filename for the key. That’s good!

00:37 Because then, you can just say first_object.upload_file() and then pass in that first_file_name.

00:48 When you run this, you shouldn’t see any errors and now that file has been uploaded to S3. Okay! So, that’s one way to do that. Now let’s look at how you can upload a file from the Bucket instance.

01:03 You should still have your first_bucket, which has the bucket name here. And from here, it’s pretty similar. You can just say first_bucket.upload_file(), and from here what you’re going to need to do is pass in a Filename, which is first_file_name, and then a key, which is also first_file_name.

01:29 Go ahead and run that. And I’ve got an error here because I left key lowercase, so let’s go back to that

01:39 and this time put a capital K.

01:44 And that was successful too. Now it may seem strange that you have to put in the filename twice, but one time is identifying what the file is and then the other time is identifying how the Key will store that file in the S3 bucket. You can kind of get an idea for this before, when we were working with the s3.Object, which identified the bucket_name and also had a key already defined.

02:09 Then when you went to actually upload the file, you had to put in that filename so that it knew which file to actually upload.

02:17 All right! That’s pretty cool! You can start uploading files to buckets that you’ve created and all from within Python. So now, the whole point of putting items into S3 is being able to download them out of S3. Let’s take a look at how to do that. Here, you’re going to go back to your s3_resource and create a new Object.

02:39 This Object will be associated with the first bucket, so pass in that first_bucket_name and then also that first_file_name.

02:48 And note that this is now looking for that key. So from here, you can call .download_file(),

02:57 and now you need to put in a file path to where you want to store it. I’m going to do some f-string formatting. In my /tmp/ folder I made a little folder called boto_test/, and I’m just going to put in first_file_name to save it in there.

03:15 Let’s run this—and I don’t see any errors. To verify that this worked, let me quit out of Python here and I’ll just list everything within that /tmp/boto_test/.

03:29 And cool! There’s that file right there. To make sure nothing funky happened with it, I can call /tmp/boto_test/and what’s that start with? An fda. And we’ll just cat that file out, and there’s the 300 f’s right there. Awesome!

03:48 Hopefully that’s not too anticlimactic. You can get an idea of how Boto3 really abstracts away a lot of these API calls and makes it very straightforward to create buckets and upload and download files to those buckets. That’s definitely the core of working with S3, but in the next video, you’re going to learn how to move objects between buckets. Thanks for watching.

Muthukumar Kusalavan on April 29, 2020

thanks a lot for the tutorial. the term Bucket() is case sensitive?

icarolinebrasil on June 27, 2020

I received an error when try to download file

>>> s3_resource.Object(first_bucket_name, first_file_name).download_file(f'/tmp/boto_test/{first_file_name}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/.local/lib/python3.6/site-packages/boto3/s3/inject.py", line 314, in object_download_file
ExtraArgs=ExtraArgs, Callback=Callback, Config=Config)

Become a Member to join the conversation.