|
| 1 | +from chainlit.data.storage_clients.base import BaseStorageClient |
| 2 | +from chainlit.logger import logger |
| 3 | +from typing import Dict, Union, Any |
| 4 | +import boto3 # type: ignore |
| 5 | + |
| 6 | + |
| 7 | +class MinioStorageClient(BaseStorageClient): |
| 8 | + """ |
| 9 | + Class to enable MinIO storage provider |
| 10 | +
|
| 11 | + params: |
| 12 | + bucket: Bucket name, should be set with public access |
| 13 | + endpoint_url: MinIO server endpoint, defaults to "http://localhost:9000" |
| 14 | + access_key: Default is "minioadmin" |
| 15 | + secret_key: Default is "minioadmin" |
| 16 | + verify_ssl: Set to True only if not using HTTP or HTTPS with self-signed SSL certificates |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, bucket: str, endpoint_url: str = 'http://localhost:9000', access_key: str = 'minioadmin', secret_key: str = 'minioadmin', verify_ssl: bool = False): |
| 20 | + |
| 21 | + self.bucket = bucket |
| 22 | + self.endpoint_url = endpoint_url |
| 23 | + |
| 24 | + try: |
| 25 | + self.client = boto3.client("s3", endpoint_url=endpoint_url, aws_access_key_id=access_key, aws_secret_access_key=secret_key, verify=verify_ssl) |
| 26 | + except Exception as e: |
| 27 | + logger.warn(f"MinioStorageClient initialization error: {e}") |
| 28 | + else: |
| 29 | + logger.info("MinioStorageClient initialized") |
| 30 | + |
| 31 | + |
| 32 | + async def upload_file(self, object_key: str, data: Union[bytes, str], mime: str = 'application/octet-stream', overwrite: bool = True) -> Dict[str, Any]: |
| 33 | + try: |
| 34 | + self.client.put_object(Bucket=self.bucket, Key=object_key, Body=data, ContentType=mime) |
| 35 | + except Exception as e: |
| 36 | + logger.warn(f"MinioStorageClient, upload_file error: {e}") |
| 37 | + return {} |
| 38 | + else: |
| 39 | + return {"object_key": object_key, "url": f"{self.endpoint_url}/{self.bucket}/{object_key}"} |
0 commit comments