How to download files from S3 Bucket using boto3 and Python

If you want to download a file from an AWS S3 Bucket using Python, then you can use the sample codes below.

The codes below use AWS SDK for Python named boto3.

boto3 provides three methods to download a file.

  1. download_file()
  2. download_fileobj() – with multipart upload
  3. get_object()

Then for each method, you can use the client class or the resource class of boto3.

Both of the classes will be used for each of the methods above.

Note: All examples will work with any Python3 environment running in Windows, MacOS or Linux operating systems.

download_file() method

The download_file() function simply downloads the S3 object and places it in the location and file name that you specify.

The examples below downloads the S3 object in the radishlogic-bucket S3 Bucket with a Key of s3_folder/photo.jpg. It then places the file in the local_folder folder with the filename of image.jpg (Filename).

boto3.client(‘s3’)

import boto3

s3_client = boto3.client('s3')

s3_client.download_file(
    Bucket='radishlogic-bucket',
    Key='s3_folder/photo.jpg',
    Filename='local_folder/image.jpg'
)

boto3.resource(‘s3’)

import boto3

s3_resource = boto3.resource('s3')

s3_object = s3_resource.Object(
    bucket_name='radishlogic-bucket',
    key='s3_folder/photo.jpg'
)

s3_object.download_file(Filename='local_folder/image.jpg')

As you may have noticed, the object or filename in S3 does not have to be the same as the filename where you will be downloading the file. This is the same for all 3 methods.

You can read more about the download_file() function in the boto3 documentation here (boto3.client) and here (boto3.resource).


download_fileobj() method

The download_fileobj() function downloads the AWS S3 object to your local computer or server, but this time it will automatically trigger a multi-part download when it reaches a certain threshold.

This is much faster than using the download_file() method since this uses a multi-part download.

Below are the sample codes for using download_fileobj() of boto3.

boto3.client(‘s3’)

import boto3

s3_client = boto3.client('s3')

with open('local_folder/image.jpeg', 'wb') as file:

    s3_client.download_fileobj(
        Bucket='radishlogic-bucket',
        Key='s3_folder/photo.jpg',
        Fileobj=file
    )

boto3.resource(‘s3’)

import boto3

s3_resource = boto3.resource('s3')

s3_object = s3_resource.Object(
    bucket_name='radishlogic-bucket',
    key='s3_folder/photo.jpg'
)

with open('local_folder/image.jpg', 'wb') as file:
    
    s3_object.download_fileobj(
        Fileobj=file
    )

You can read more about boto3’s download_fileobj() method here (client) and here (resource).


get_object() method

The get_object() function of boto3 is more for the use of accessing and reading an S3 object then processing it inside the Python Script. Since it can be used to download a file to your local computer or server, I will also show how to do it here.

Below are the example codes to download a file from S3 going to your computer using Python and boto3.

boto3.client(‘s3’)

import boto3

s3_client = boto3.client('s3')

# Get the file inside the S3 Bucket
s3_response = s3_client.get_object(
    Bucket='radishlogic-bucket',
    Key='s3_folder/photo.jpg'
)

# Get the Body object in the S3 get_object() response
s3_object_body = s3_response.get('Body')

# Read the data in bytes format
content = s3_object_body.read()

with open('local_folder/image.jpg', 'wb') as file:

    file.write(content)

boto3.resource(‘s3’)

import boto3

# Initialize boto3 to use S3 resource
s3_resource = boto3.resource('s3')

# Get the object from the S3 Bucket
s3_object = s3_resource.Object(
    bucket_name='radishlogic-bucket', 
    key='s3_folder/photo.jpg'
)

# Get the response from get_object()
s3_response = s3_object.get()

# Get the Body object in the S3 get_object() response
s3_object_body = s3_response.get('Body')

# Read the data in bytes format
content = s3_object_body.read()

with open('local_folder/image.jpg', 'wb') as file:
    file.write(content)

You can read boto3’s documentation about get_object() method here (client) and here (resource).


Recommendation

Among the 3 methods of downloading files from S3, I would recommend using the download_fileobj() method since it is much faster compared to the download_file() and get_object() method.


We hope that this post helped you download your file from S3 going to your local computer or server using Python and boto3.

Let us know your experience in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.