Skip to content

AWS S3 Utilities

audio_classifier.s3_utils

get_audio_file(audio_url)

Get audio file from AWS S3, in bucket.

Parameters:

Name Type Description Default
audio_url str

Audio url in S3.

required

Raises:

Type Description
exc

Error fetching file.

Returns:

Name Type Description
_type_

Pre-signed URL pointing to the audio file of the JSON annotation.

Source code in src/audio_classifier/s3_utils.py
def get_audio_file(audio_url: str):
    """Get audio file from AWS S3, in `bucket`.

    Args:
        audio_url (str): Audio url in S3.

    Raises:
        exc: Error fetching file.

    Returns:
        _type_: Pre-signed URL pointing to the audio file of the JSON annotation.
    """
    audio_url = audio_url.replace("s3://", "")
    bucket, key = audio_url.split("/", 1)
    try:
        s3_source_signed_url = s3_client.generate_presigned_url(
            "get_object",
            Params={"Bucket": bucket, "Key": key},
            ExpiresIn=3600,
        )
    except Exception as exc:
        print(f"Failed to fetch key: {key}")
        raise exc
    else:
        return s3_source_signed_url