import re

import boto3


def generate_presigned_url(url):
    """
    Generate a presigned URL, if necessary (e.g., s3).

    :param url: An unsigned URL.
    :return: The presigned URL.
    """
    if url.startswith("s3"):
        client = boto3.client("s3")

        bucket_name, filename = re.compile("s3://([\w\d\-\.]+)/(.*)").search(url).groups()

        url = client.generate_presigned_url(
            "get_object",
            Params={"Bucket": bucket_name,
                    "Key": filename.replace("+", " ")}
        )

    return url
