dify/api/extensions/storage/volcengine_tos_storage.py

67 lines
2.6 KiB
Python
Raw Permalink Normal View History

2024-09-10 09:19:47 +08:00
from collections.abc import Generator
import tos
2024-09-10 09:19:47 +08:00
from configs import dify_config
2024-09-10 09:19:47 +08:00
from extensions.storage.base_storage import BaseStorage
class VolcengineTosStorage(BaseStorage):
2024-09-10 09:19:47 +08:00
"""Implementation for Volcengine TOS storage."""
def __init__(self):
super().__init__()
if not dify_config.VOLCENGINE_TOS_ACCESS_KEY:
raise ValueError("VOLCENGINE_TOS_ACCESS_KEY is not set")
if not dify_config.VOLCENGINE_TOS_SECRET_KEY:
raise ValueError("VOLCENGINE_TOS_SECRET_KEY is not set")
if not dify_config.VOLCENGINE_TOS_ENDPOINT:
raise ValueError("VOLCENGINE_TOS_ENDPOINT is not set")
if not dify_config.VOLCENGINE_TOS_REGION:
raise ValueError("VOLCENGINE_TOS_REGION is not set")
self.bucket_name = dify_config.VOLCENGINE_TOS_BUCKET_NAME
2024-09-10 09:19:47 +08:00
self.client = tos.TosClientV2(
ak=dify_config.VOLCENGINE_TOS_ACCESS_KEY,
sk=dify_config.VOLCENGINE_TOS_SECRET_KEY,
endpoint=dify_config.VOLCENGINE_TOS_ENDPOINT,
region=dify_config.VOLCENGINE_TOS_REGION,
2024-09-10 09:19:47 +08:00
)
def save(self, filename, data):
if not self.bucket_name:
raise ValueError("VOLCENGINE_TOS_BUCKET_NAME is not set")
2024-09-10 09:19:47 +08:00
self.client.put_object(bucket=self.bucket_name, key=filename, content=data)
def load_once(self, filename: str) -> bytes:
if not self.bucket_name:
raise FileNotFoundError("VOLCENGINE_TOS_BUCKET_NAME is not set")
2024-09-10 09:19:47 +08:00
data = self.client.get_object(bucket=self.bucket_name, key=filename).read()
2024-12-24 18:38:51 +08:00
if not isinstance(data, bytes):
raise TypeError(f"Expected bytes, got {type(data).__name__}")
2024-09-10 09:19:47 +08:00
return data
def load_stream(self, filename: str) -> Generator:
if not self.bucket_name:
raise FileNotFoundError("VOLCENGINE_TOS_BUCKET_NAME is not set")
response = self.client.get_object(bucket=self.bucket_name, key=filename)
while chunk := response.read(4096):
yield chunk
2024-09-10 09:19:47 +08:00
def download(self, filename, target_filepath):
if not self.bucket_name:
raise ValueError("VOLCENGINE_TOS_BUCKET_NAME is not set")
2024-09-10 09:19:47 +08:00
self.client.get_object_to_file(bucket=self.bucket_name, key=filename, file_path=target_filepath)
def exists(self, filename):
if not self.bucket_name:
return False
2024-09-10 09:19:47 +08:00
res = self.client.head_object(bucket=self.bucket_name, key=filename)
if res.status_code != 200:
return False
return True
def delete(self, filename):
if not self.bucket_name:
return
2024-09-10 09:19:47 +08:00
self.client.delete_object(bucket=self.bucket_name, key=filename)