"""Boto3 S3 connector.""" from functools import cache import boto3 from botocore.exceptions import ClientError from deliveryhistory import config @cache def get_s3_client(): """Connect to S3. Returns: botocore.client.S3: client object providing access to S3. """ return boto3.session.Session().client('s3', region_name=config.AWS_REGION) def get_object_body(bucket, key): """Download the body of an S3 object. Args: bucket (str): the S3 bucket name key (str): the S3 object key Returns: bytes: the object body, or None if the object does not exist """ try: result = get_s3_client().get_object(Bucket=bucket, Key=key) except ClientError: return None return result['Body'].read()