dify/api/extensions/ext_storage.py

101 lines
3.4 KiB
Python
Raw Normal View History

2024-09-19 17:34:12 +08:00
import logging
from collections.abc import Generator
from typing import Union
2023-05-15 08:51:32 +08:00
from flask import Flask
2024-04-29 18:22:03 +08:00
from extensions.storage.aliyun_storage import AliyunStorage
from extensions.storage.azure_storage import AzureStorage
from extensions.storage.google_storage import GoogleStorage
from extensions.storage.huawei_storage import HuaweiStorage
2024-04-29 18:22:03 +08:00
from extensions.storage.local_storage import LocalStorage
from extensions.storage.oci_storage import OCIStorage
2024-04-29 18:22:03 +08:00
from extensions.storage.s3_storage import S3Storage
from extensions.storage.tencent_storage import TencentStorage
2024-09-10 09:19:47 +08:00
from extensions.storage.volcengine_storage import VolcengineStorage
2023-05-15 08:51:32 +08:00
class Storage:
def __init__(self):
2024-04-29 18:22:03 +08:00
self.storage_runner = None
2023-05-15 08:51:32 +08:00
def init_app(self, app: Flask):
storage_type = app.config.get("STORAGE_TYPE")
if storage_type == "s3":
self.storage_runner = S3Storage(app=app)
elif storage_type == "azure-blob":
self.storage_runner = AzureStorage(app=app)
elif storage_type == "aliyun-oss":
self.storage_runner = AliyunStorage(app=app)
elif storage_type == "google-storage":
self.storage_runner = GoogleStorage(app=app)
elif storage_type == "tencent-cos":
self.storage_runner = TencentStorage(app=app)
elif storage_type == "oci-storage":
self.storage_runner = OCIStorage(app=app)
elif storage_type == "huawei-obs":
self.storage_runner = HuaweiStorage(app=app)
2024-09-10 09:19:47 +08:00
elif storage_type == "volcengine-tos":
self.storage_runner = VolcengineStorage(app=app)
2023-05-15 08:51:32 +08:00
else:
2024-04-29 18:22:03 +08:00
self.storage_runner = LocalStorage(app=app)
2023-05-15 08:51:32 +08:00
def save(self, filename, data):
2024-09-19 17:34:12 +08:00
try:
self.storage_runner.save(filename, data)
except Exception as e:
logging.exception("Failed to save file: %s", e)
raise e
2023-05-15 08:51:32 +08:00
def load(self, filename: str, stream: bool = False) -> Union[bytes, Generator]:
2024-09-19 17:34:12 +08:00
try:
if stream:
return self.load_stream(filename)
else:
return self.load_once(filename)
except Exception as e:
logging.exception("Failed to load file: %s", e)
raise e
def load_once(self, filename: str) -> bytes:
2024-09-19 17:34:12 +08:00
try:
return self.storage_runner.load_once(filename)
except Exception as e:
logging.exception("Failed to load_once file: %s", e)
raise e
2023-05-15 08:51:32 +08:00
def load_stream(self, filename: str) -> Generator:
2024-09-19 17:34:12 +08:00
try:
return self.storage_runner.load_stream(filename)
except Exception as e:
logging.exception("Failed to load_stream file: %s", e)
raise e
2023-05-15 08:51:32 +08:00
def download(self, filename, target_filepath):
2024-09-19 17:34:12 +08:00
try:
self.storage_runner.download(filename, target_filepath)
except Exception as e:
logging.exception("Failed to download file: %s", e)
raise e
2023-05-15 08:51:32 +08:00
def exists(self, filename):
2024-09-19 17:34:12 +08:00
try:
return self.storage_runner.exists(filename)
except Exception as e:
logging.exception("Failed to check file exists: %s", e)
raise e
2023-05-15 08:51:32 +08:00
def delete(self, filename):
2024-09-19 17:34:12 +08:00
try:
return self.storage_runner.delete(filename)
except Exception as e:
logging.exception("Failed to delete file: %s", e)
raise e
2023-05-15 08:51:32 +08:00
storage = Storage()
def init_app(app: Flask):
storage.init_app(app)