Downloading Files From S3 in Python

Amazon Web Services provides Boto3 S3 SDK for Python. The Boto3 SDK provides methods for uploading and downloading files from S3 buckets.

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

Installing Boto3 AWS S3 SDK

Install the latest version of Boto3 S3 SDK using the following command:


pip install boto3

Downloading Files from S3

To download files from S3, use download_fileobj(bucket, key, filename) method that downloads a file as an object to a file-like object.

Example

import boto3
from botocore.exceptions import ClientError

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


def download_my_file(bucket, folder, file_name):
    key = key = folder+"/"+file_name
    with open(file_name, 'wb') as f:
        s3_client.download_fileobj(bucket, key, f)

#Downloading file
download_my_file("bucket-name", "download-from-subfolder-name", "test.html")