import re
from magic_pdf.libs.config_reader import get_s3_config_dict

__re_s3_path = re.compile("^s3a?://([^/]+)(?:/(.*))?$")


def get_s3_config(path):
    bucket_name = split_s3_path(path)[0] if path else ""
    return get_s3_config_dict(bucket_name)


def split_s3_path(path: str):
    "split bucket and key from path"
    m = __re_s3_path.match(path)
    if m is None:
        return "", ""
    return m.group(1), (m.group(2) or "")
