def s3_url_maker()

in taskcat/_common_utils.py [0:0]


def s3_url_maker(bucket, key, s3_client, autobucket=False):
    retries = 10
    while True:
        try:
            try:
                response = s3_client.get_bucket_location(Bucket=bucket)
                location = response["LocationConstraint"]
            except ClientError as e:
                if e.response["Error"]["Code"] != "AccessDenied":
                    raise
                resp = requests.get(f"https://{bucket}.s3.amazonaws.com/{key}")
                location = resp.headers.get("x-amz-bucket-region")
                if not location:
                    # pylint: disable=raise-missing-from
                    raise TaskCatException(
                        f"failed to discover region for bucket {bucket}"
                    )
            break
        except s3_client.exceptions.NoSuchBucket:
            if not autobucket or retries < 1:
                raise
            retries -= 1
            sleep(5)

    # default case for us-east-1 which returns no location
    url = f"https://{bucket}.s3.us-east-1.amazonaws.com/{key}"
    if location:
        domain = get_s3_domain(location)
        url = f"https://{bucket}.s3.{location}.{domain}/{key}"
    return url