import boto3
import botocore
def get_file_text_from_s3(bucket_name, s3_path, filename):
"""Gets a file from AWS s3 and returns its text. Make sure it has IAM permissions to access file."""
if not filename or not isinstance(filename, str):
raise Exception('Filename must be a non-empty string.')
s3_path = s3_path + filename
try:
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket_name, Key=s3_path)
return obj['Body'].read().decode('utf-8')
except botocore.exceptions.ClientError as error:
# Maybe log the error here
return False
Posted: March 21, 2023
Return to the snippets listing