Read A File From S3 With The boto3 Python Module
December - 2021
Here's how to read an S3 file with either a .client
or .resource
object:
boto3.client('s3')
#!/usr/bin/env python3
import boto3
s3 = boto3.client('s3')
data = s3.get_object(
Bucket="bucket_name",
Key="key_path_name"
)
text_contents = data['Body'].read().decode('utf-8')
print(text_contents)
boto3.resource('s3')
#!/usr/bin/env python3
import boto3
s3 = boto3.resource('s3')
s3_object = s3.Object('bucket_name', 'key_path_name')
text_content = s3_object.get()['Body'].read().decode('utf-8')
print(text_content)