-
Notifications
You must be signed in to change notification settings - Fork 931
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Refactor storage_clients.py from a script to a module inside chainlit.data * Add pytest on S3StorageClient + add moto in tests dependencies * Moved BaseStorageClient from from chainlit.data.base.py to chainlit.data.storage_clients.base.py Co-authored-by: jl1andricca <[email protected]>
- Loading branch information
Showing
13 changed files
with
287 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any, Dict, Union | ||
|
||
|
||
class BaseStorageClient(ABC): | ||
"""Base class for non-text data persistence like Azure Data Lake, S3, Google Storage, etc.""" | ||
|
||
@abstractmethod | ||
async def upload_file( | ||
self, | ||
object_key: str, | ||
data: Union[bytes, str], | ||
mime: str = "application/octet-stream", | ||
overwrite: bool = True, | ||
) -> Dict[str, Any]: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Any, Dict, Union | ||
|
||
import boto3 # type: ignore | ||
from chainlit.data.storage_clients.base import BaseStorageClient | ||
from chainlit.logger import logger | ||
|
||
|
||
class S3StorageClient(BaseStorageClient): | ||
""" | ||
Class to enable Amazon S3 storage provider | ||
""" | ||
|
||
def __init__(self, bucket: str): | ||
try: | ||
self.bucket = bucket | ||
self.client = boto3.client("s3") | ||
logger.info("S3StorageClient initialized") | ||
except Exception as e: | ||
logger.warn(f"S3StorageClient initialization error: {e}") | ||
|
||
async def upload_file( | ||
self, | ||
object_key: str, | ||
data: Union[bytes, str], | ||
mime: str = "application/octet-stream", | ||
overwrite: bool = True, | ||
) -> Dict[str, Any]: | ||
try: | ||
self.client.put_object( | ||
Bucket=self.bucket, Key=object_key, Body=data, ContentType=mime | ||
) | ||
url = f"https://{self.bucket}.s3.amazonaws.com/{object_key}" | ||
return {"object_key": object_key, "url": url} | ||
except Exception as e: | ||
logger.warn(f"S3StorageClient, upload_file error: {e}") | ||
return {} |
Oops, something went wrong.