Downloading Files From S3 in Python

  • Last updated Apr 25, 2024

In this tutorial, you will learn how to download files from S3 using the AWS Boto3 library in Python.

You can also learn how to upload files to AWS S3 here.

Install Boto3 Python library using the following command:

pip install boto3
Here's an example code to download files from S3 using download_fileobj(bucket_name, key, filename) method that downloads a file as an object to a file-like object:

import boto3
from botocore.exceptions import ClientError

ACCESS_SECRET = "your aws access secret"
ACCESS_KEY = "your aws access key"

s3_client = boto3.client('s3', region_name='us-east-1', aws_access_key_id=ACCESS_KEY,

bucket_name = "bucket-one"

file_name = "example.txt"

#file to download from s3
key = "folder/"+file_name

#download file
    with open(file_name, 'wb') as f:
        s3_client.download_fileobj(bucket_name, key, f)
except ClientError as ex:
    print("Error: ", ex)