mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-30 15:24:17 +00:00
feat: add s3 provider to files API
Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
parent
e3ad17ec5e
commit
749cbcca31
17 changed files with 614 additions and 132 deletions
76
llama_stack/providers/remote/files/object/s3/persistence.py
Normal file
76
llama_stack/providers/remote/files/object/s3/persistence.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
# All rights reserved.
|
||||
#
|
||||
# This source code is licensed under the terms described in the LICENSE file in
|
||||
# the root directory of this source tree.
|
||||
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from llama_stack.apis.files.files import FileUploadResponse
|
||||
from llama_stack.providers.utils.kvstore import KVStore
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class UploadSessionInfo(BaseModel):
|
||||
"""Information about an upload session."""
|
||||
|
||||
upload_id: str
|
||||
bucket: str
|
||||
key: str
|
||||
mime_type: str
|
||||
size: int
|
||||
url: str
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class S3FilesPersistence:
|
||||
def __init__(self, kvstore: KVStore):
|
||||
self._kvstore = kvstore
|
||||
self._store = None
|
||||
|
||||
async def _get_store(self) -> KVStore:
|
||||
"""Get the kvstore instance, initializing it if needed."""
|
||||
if self._store is None:
|
||||
self._store = await anext(self._kvstore)
|
||||
return self._store
|
||||
|
||||
async def store_upload_session(
|
||||
self, session_info: FileUploadResponse, bucket: str, key: str, mime_type: str, size: int
|
||||
):
|
||||
"""Store upload session information."""
|
||||
upload_info = UploadSessionInfo(
|
||||
upload_id=session_info.id,
|
||||
bucket=bucket,
|
||||
key=key,
|
||||
mime_type=mime_type,
|
||||
size=size,
|
||||
url=session_info.url,
|
||||
created_at=datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
store = await self._get_store()
|
||||
await store.set(
|
||||
key=f"upload_session:{session_info.id}",
|
||||
value=upload_info.model_dump_json(),
|
||||
)
|
||||
|
||||
async def get_upload_session(self, upload_id: str) -> UploadSessionInfo | None:
|
||||
"""Get upload session information."""
|
||||
store = await self._get_store()
|
||||
value = await store.get(
|
||||
key=f"upload_session:{upload_id}",
|
||||
)
|
||||
if not value:
|
||||
return None
|
||||
|
||||
return UploadSessionInfo(**json.loads(value))
|
||||
|
||||
async def delete_upload_session(self, upload_id: str) -> None:
|
||||
"""Delete upload session information."""
|
||||
store = await self._get_store()
|
||||
await store.delete(key=f"upload_session:{upload_id}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue