Skip to content

Commit

Permalink
Add ability to replace the host with a different base in MinIO
Browse files Browse the repository at this point in the history
  • Loading branch information
JBorrow committed Jan 9, 2025
1 parent 2101970 commit 2469d7f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ actually provide file storage for your server, with MongoDB handling the metadat

There are a number of important configuration variables:

- `MINIO_URL`: hostname of the MINIO server.
- `MINIO_URL`: hostname of the MINIO server as seen by the server.
- `MINIO_ACCESS`: the MINIO access token (username).
- `MINIO_SECRET`: the MINIO access secret (password).
- `MINIO_PRESIGN_URL`: hostname of the MINIO server as seen by external clients.
- `MONGO_URI`: the full URI for the mongo instance including password.
- `TITLE`: the title of the HIPPO instance.
- `DESCRIPTION`: the description of the HIPPO instance.
Expand Down
2 changes: 1 addition & 1 deletion hippoclient/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def settings_customise_sources(
return (
init_settings,
dotenv_settings,
JsonConfigSettingsSource(settings_cls),
env_settings,
JsonConfigSettingsSource(settings_cls),
file_secret_settings,
)

Expand Down
1 change: 1 addition & 0 deletions hipposerve/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class Settings(BaseSettings):
minio_url: str
minio_presign_url: str | None
minio_access: str
minio_secret: str

Expand Down
20 changes: 18 additions & 2 deletions hipposerve/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@
from pydantic import BaseModel, ConfigDict


def replace_host(url: str, old: str, new: str | None) -> str:
"""
Replaces the host in a URL.
"""

if new is not None:
return url.replace(old, new)
else:
return url


class Storage(BaseModel):
url: str
access_key: str
secret_key: str
presigned_url: str | None = None

client: Minio | None = None
expires: datetime.timedelta = datetime.timedelta(days=1)
Expand Down Expand Up @@ -50,7 +62,7 @@ def put(self, name: str, uploader: str, uuid: str, bucket: str) -> str:

self.bucket(name=bucket)

return self.client.presigned_put_object(
base_url = self.client.presigned_put_object(
bucket_name=bucket,
object_name=self.object_name(
filename=name,
Expand All @@ -60,6 +72,8 @@ def put(self, name: str, uploader: str, uuid: str, bucket: str) -> str:
expires=self.expires,
)

return replace_host(base_url, old=self.url, new=self.presigned_url)

def confirm(self, name: str, uploader: str, uuid: str, bucket: str) -> bool:
"""
Checks whether an object exists.
Expand Down Expand Up @@ -88,7 +102,7 @@ def get(self, name: str, uploader: str, uuid: str, bucket: str) -> str:

self.bucket(name=bucket)

return self.client.presigned_get_object(
base_url = self.client.presigned_get_object(
bucket_name=bucket,
object_name=self.object_name(
filename=name,
Expand All @@ -98,6 +112,8 @@ def get(self, name: str, uploader: str, uuid: str, bucket: str) -> str:
expires=self.expires,
)

return replace_host(base_url, old=self.url, new=self.presigned_url)

def delete(self, name: str, uploader: str, uuid: str, bucket: str) -> str:
"""
Deletes an object from the bucket.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static = ["hipposerve/web/static/*.svg", "hipposerve/web/templates/*.html"]

[project]
name = "hipposerve"
version = "0.0.1a3"
version = "0.0.1a4"
requires-python = ">=3.11"
dependencies = [
"pydantic",
Expand Down

0 comments on commit 2469d7f

Please sign in to comment.