Downloading File Content as String From S3 in Python

In this tutorial, we will show you how to download file content as string from S3 using the AWS Boto3 library in Python.

Install Boto3 Python library using the following command:

pip install boto3

The following is an example code to download file content as string from an S3 bucket:

import boto3
from botocore.exceptions import ClientError
import io

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 path in s3
key = "folder/"+file_name

# download file
    str_content = ""
    bytes_buffer = io.BytesIO()
        Bucket=bucket_name, Key=key, Fileobj=bytes_buffer)
    byte_value = bytes_buffer.getvalue()
    str_content = byte_value.decode('utf-8')
    print("File content = ", str_content)
except ClientError as ex:
    print("Error: ", ex)