Skip to content

Commit

Permalink
Relax S3 URI parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tabletcorry authored Nov 28, 2022
1 parent 9e99a6b commit ab1247f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions takahe/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
BASE_DIR = Path(__file__).resolve().parent.parent


class MediaBackendUrl(AnyUrl):
host_required = False
allowed_schemes = {"s3", "gcs", "local"}


def as_bool(v: Optional[Union[str, List[str]]]):
if v is None:
return False
Expand Down Expand Up @@ -84,7 +89,7 @@ class Settings(BaseSettings):

MEDIA_URL: str = "/media/"
MEDIA_ROOT: str = str(BASE_DIR / "media")
MEDIA_BACKEND: Optional[AnyUrl] = None
MEDIA_BACKEND: Optional[MediaBackendUrl] = None

PGHOST: Optional[str] = None
PGPORT: Optional[int] = 5432
Expand Down Expand Up @@ -299,10 +304,12 @@ class Config:
elif parsed.scheme == "s3":
DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
AWS_STORAGE_BUCKET_NAME = parsed.path.lstrip("/")
AWS_ACCESS_KEY_ID = parsed.username
AWS_SECRET_ACCESS_KEY = parsed.password
port = parsed.port or 443
AWS_S3_ENDPOINT_URL = f"https://{parsed.hostname}:{port}"
if parsed.username is not None:
AWS_ACCESS_KEY_ID = parsed.username
AWS_SECRET_ACCESS_KEY = parsed.password
if parsed.hostname is not None:
port = parsed.port or 443
AWS_S3_ENDPOINT_URL = f"https://{parsed.hostname}:{port}"
else:
raise ValueError(f"Unsupported media backend {parsed.scheme}")

Expand Down

0 comments on commit ab1247f

Please sign in to comment.