24 lines
665 B
Python
24 lines
665 B
Python
import json
|
|
|
|
|
|
class S3FileReader:
|
|
"""
|
|
An S3 client along with a few reading utils.
|
|
"""
|
|
|
|
def __init__(self, s3_client):
|
|
self.s3_client = s3_client
|
|
|
|
def read_json_from_s3_file(self, bucket: str, key: str) -> dict:
|
|
"""
|
|
Read a JSON file from an S3 location and return contents as a dict.
|
|
|
|
:param bucket: the name of the bucket where the file is stored.
|
|
:param key: the path to the file within the bucket.
|
|
:return: the file contents.
|
|
"""
|
|
return json.loads(
|
|
self.s3_client.get_object(Bucket=bucket, Key=key)["Body"]
|
|
.read()
|
|
.decode("utf-8")
|
|
)
|