Skip to content

AWS S3 Utilities

audio_recording_logger.s3_utils

S3Client

Source code in src/audio_recording_logger/s3_utils.py
class S3Client:
    def __init__(self, region_name="us-east-1"):
        """AWS S3 Client Constructor.

        Args:
            region_name (str, optional): AWS S3 region. Defaults to "us-east-1".
        """
        self.client = boto3.client("s3", region_name=region_name)
        self.resource = boto3.resource("s3", region_name=region_name)

    def get_object(self, bucket: str, key: str) -> Any:
        """Gets object from S3.

        Args:
            bucket (str): S3 bucket name.
            key (str): Key to file in bucket.

        Returns:
            Any: S3 Object retrieved.
        """
        try:
            s3_object = self.client.get_object(Bucket=bucket, Key=key)["Body"].read()
        except Exception:
            return None
        else:
            return s3_object

__init__(region_name='us-east-1')

AWS S3 Client Constructor.

Parameters:

Name Type Description Default
region_name str

AWS S3 region. Defaults to "us-east-1".

'us-east-1'
Source code in src/audio_recording_logger/s3_utils.py
def __init__(self, region_name="us-east-1"):
    """AWS S3 Client Constructor.

    Args:
        region_name (str, optional): AWS S3 region. Defaults to "us-east-1".
    """
    self.client = boto3.client("s3", region_name=region_name)
    self.resource = boto3.resource("s3", region_name=region_name)

get_object(bucket, key)

Gets object from S3.

Parameters:

Name Type Description Default
bucket str

S3 bucket name.

required
key str

Key to file in bucket.

required

Returns:

Name Type Description
Any Any

S3 Object retrieved.

Source code in src/audio_recording_logger/s3_utils.py
def get_object(self, bucket: str, key: str) -> Any:
    """Gets object from S3.

    Args:
        bucket (str): S3 bucket name.
        key (str): Key to file in bucket.

    Returns:
        Any: S3 Object retrieved.
    """
    try:
        s3_object = self.client.get_object(Bucket=bucket, Key=key)["Body"].read()
    except Exception:
        return None
    else:
        return s3_object