Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
volcengine_tos_storage.py45 linesDownload Raw Back to storage
1from collections.abc import Generator2 3import tos4 5from configs import dify_config6from extensions.storage.base_storage import BaseStorage7 8 9class VolcengineTosStorage(BaseStorage):10    """Implementation for Volcengine TOS storage."""11 12    def __init__(self):13        super().__init__()14        self.bucket_name = dify_config.VOLCENGINE_TOS_BUCKET_NAME15        self.client = tos.TosClientV2(16            ak=dify_config.VOLCENGINE_TOS_ACCESS_KEY,17            sk=dify_config.VOLCENGINE_TOS_SECRET_KEY,18            endpoint=dify_config.VOLCENGINE_TOS_ENDPOINT,19            region=dify_config.VOLCENGINE_TOS_REGION,20        )21 22    def save(self, filename, data):23        self.client.put_object(bucket=self.bucket_name, key=filename, content=data)24 25    def load_once(self, filename: str) -> bytes:26        data = self.client.get_object(bucket=self.bucket_name, key=filename).read()27        return data28 29    def load_stream(self, filename: str) -> Generator:30        response = self.client.get_object(bucket=self.bucket_name, key=filename)31        while chunk := response.read(4096):32            yield chunk33 34    def download(self, filename, target_filepath):35        self.client.get_object_to_file(bucket=self.bucket_name, key=filename, file_path=target_filepath)36 37    def exists(self, filename):38        res = self.client.head_object(bucket=self.bucket_name, key=filename)39        if res.status_code != 200:40            return False41        return True42 43    def delete(self, filename):44        self.client.delete_object(bucket=self.bucket_name, key=filename)45