Skip to content

Commit 8be4510

Browse files
feat: add aioboto3
1 parent 6ef2e0e commit 8be4510

File tree

5 files changed

+507
-7
lines changed

5 files changed

+507
-7
lines changed

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ services:
3939
RUSTFS_ACCESS_KEY: rustfsadmin
4040
RUSTFS_SECRET_KEY: rustfsadmin
4141
RUSTFS_CONSOLE_ENABLE: "true"
42-
RUSTFS_SERVER_DOMAINS: "example.com"
4342
command:
4443
- --address
4544
- :9000

src/backend/app/deps.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from sqlmodel.ext.asyncio.session import AsyncSession
2-
from typing import Annotated
2+
from typing import Annotated, Any
33
from app.db import get_session
44
from fastapi import Depends, HTTPException, status
55
import jwt
@@ -10,6 +10,9 @@
1010
from app.schemas.token import TokenPayload
1111
from app.settings import settings
1212
from app import security
13+
import aioboto3
14+
from botocore.exceptions import ClientError
15+
from botocore.client import BaseClient
1316

1417
bearer_scheme = HTTPBearer(auto_error=True)
1518

@@ -31,11 +34,28 @@ async def get_current_user(session: SessionDep, token: TokenDep) -> User:
3134
return user
3235

3336

37+
async def get_s3_client():
38+
session = aioboto3.Session()
39+
async with session.client(
40+
"s3",
41+
endpoint_url=settings.RUSTFS_ENDPOINT_URL, # RustFS S3 API
42+
aws_access_key_id=settings.RUSTFS_ACCESS_KEY,
43+
aws_secret_access_key=settings.RUSTFS_SECRET_ACCESS_KEY,
44+
) as s3_client:
45+
# Ensure bucket exists
46+
try:
47+
await s3_client.head_bucket(Bucket=settings.RUSTFS_BUCKET_NAME)
48+
except ClientError:
49+
await s3_client.create_bucket(Bucket=settings.RUSTFS_BUCKET_NAME)
50+
yield s3_client
51+
52+
3453
SessionDep = Annotated[AsyncSession, Depends(get_session)]
3554
TokenDep = Annotated[
3655
HTTPAuthorizationCredentials,
3756
Depends(bearer_scheme),
3857
]
3958
CurrentUser = Annotated[User, Depends(get_current_user)]
59+
S3Dep = Annotated[BaseClient, Depends(get_s3_client)]
4060

41-
__all__ = ["SessionDep", "CurrentUser", "TokenDep"]
61+
__all__ = ["SessionDep", "CurrentUser", "TokenDep", "S3Dep"]

src/backend/app/settings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import secrets
2-
31
from pydantic import PostgresDsn, computed_field
42
from pydantic_settings import BaseSettings, SettingsConfigDict
53

@@ -48,6 +46,11 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn | str:
4846
ACCESS_TOKEN_EXPIRE_MINUTES: int = (
4947
60 * 60 * 24 * 8 # 60 minutes * 24 hours * 8 days = 8 days
5048
)
49+
# RustFS
50+
RUSTFS_ENDPOINT_URL: str = "http://localhost:9000"
51+
RUSTFS_ACCESS_KEY: str = "rustfsadmin"
52+
RUSTFS_SECRET_ACCESS_KEY: str = "rustfsadmin"
53+
RUSTFS_BUCKET_NAME: str = "chithi"
5154

5255

5356
settings = Settings() # type: ignore

src/backend/pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ dependencies = [
2323
"alembic>=1.17.2",
2424
# Cli
2525
"typer>=0.20.1",
26+
# Rustfs Upload
27+
"aioboto3>=15.5.0",
2628
]
2729

2830
[dependency-groups]
29-
dev = ["fastapi-cli>=0.0.16"]
31+
dev = ["fastapi-cli>=0.0.16", "types-aioboto3>=15.5.0"]
3032

3133
[tool.setuptools]
3234
packages = ["app"]

0 commit comments

Comments
 (0)