Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
baidu_obs_storage.py57 linesDownload Raw Back to storage
1import base642import hashlib3from collections.abc import Generator4 5from baidubce.auth.bce_credentials import BceCredentials6from baidubce.bce_client_configuration import BceClientConfiguration7from baidubce.services.bos.bos_client import BosClient8 9from configs import dify_config10from extensions.storage.base_storage import BaseStorage11 12 13class BaiduObsStorage(BaseStorage):14    """Implementation for Baidu OBS storage."""15 16    def __init__(self):17        super().__init__()18        self.bucket_name = dify_config.BAIDU_OBS_BUCKET_NAME19        client_config = BceClientConfiguration(20            credentials=BceCredentials(21                access_key_id=dify_config.BAIDU_OBS_ACCESS_KEY,22                secret_access_key=dify_config.BAIDU_OBS_SECRET_KEY,23            ),24            endpoint=dify_config.BAIDU_OBS_ENDPOINT,25        )26 27        self.client = BosClient(config=client_config)28 29    def save(self, filename, data):30        md5 = hashlib.md5()31        md5.update(data)32        content_md5 = base64.standard_b64encode(md5.digest())33        self.client.put_object(34            bucket_name=self.bucket_name, key=filename, data=data, content_length=len(data), content_md5=content_md535        )36 37    def load_once(self, filename: str) -> bytes:38        response = self.client.get_object(bucket_name=self.bucket_name, key=filename)39        return response.data.read()40 41    def load_stream(self, filename: str) -> Generator:42        response = self.client.get_object(bucket_name=self.bucket_name, key=filename).data43        while chunk := response.read(4096):44            yield chunk45 46    def download(self, filename, target_filepath):47        self.client.get_object_to_file(bucket_name=self.bucket_name, key=filename, file_name=target_filepath)48 49    def exists(self, filename):50        res = self.client.get_object_meta_data(bucket_name=self.bucket_name, key=filename)51        if res is None:52            return False53        return True54 55    def delete(self, filename):56        self.client.delete_object(bucket_name=self.bucket_name, key=filename)57