From 901bee072abd5fffeea32cee68af53db74724a7a Mon Sep 17 00:00:00 2001 From: Ahmada Yusril Date: Tue, 30 Apr 2024 18:35:16 +0900 Subject: [PATCH] feat: add MinIO as storage provider --- backend/chainlit/data/storage_clients.py | 31 +++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/backend/chainlit/data/storage_clients.py b/backend/chainlit/data/storage_clients.py index 7242b76e00..7424afa172 100644 --- a/backend/chainlit/data/storage_clients.py +++ b/backend/chainlit/data/storage_clients.py @@ -11,7 +11,7 @@ class AzureStorageClient(BaseStorageClient): """ Class to enable Azure Data Lake Storage (ADLS) Gen2 - parms: + params: account_url: "https://.dfs.core.windows.net" credential: Access credential (AzureKeyCredential) sas_token: Optionally include SAS token to append to urls @@ -56,3 +56,32 @@ async def upload_file(self, object_key: str, data: Union[bytes, str], mime: str except Exception as e: logger.warn(f"S3StorageClient, upload_file error: {e}") return {} + +class MinioStorageClient(BaseStorageClient): + """ + Class to enable MinIO storage provider + + params: + bucket: Bucket name, should be set with public access + endpoint_url: MinIO server endpoint, defaults to "http://localhost:9000" + aws_access_key_id: Default is "minioadmin" + aws_secret_access_key: Default is "minioadmin" + verify_ssl: Set to True only if not using HTTP or HTTPS with self-signed SSL certificates + """ + def __init__(self, bucket: str, endpoint_url: str = 'http://localhost:9000', aws_access_key_id: str = 'minioadmin', aws_secret_access_key: str = 'minioadmin', verify_ssl: bool = False): + try: + self.bucket = bucket + self.endpoint_url = endpoint_url + self.client = boto3.client("s3", endpoint_url=self.endpoint_url, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, verify=verify_ssl) + logger.info("MinioStorageClient initialized") + except Exception as e: + logger.warn(f"MinioStorageClient 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"{self.endpoint_url}/{self.bucket}/{object_key}" + return {"object_key": object_key, "url": url} + except Exception as e: + logger.warn(f"MinioStorageClient, upload_file error: {e}") + return {}