Skip to content

Commit

Permalink
refactor: introduce storage factory and speed up api startup by impor…
Browse files Browse the repository at this point in the history
…ting storage client on demand (langgenius#9086)
  • Loading branch information
bowenliang123 authored Oct 9, 2024
1 parent 5fcd614 commit b360feb
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 51 deletions.
80 changes: 49 additions & 31 deletions api/extensions/ext_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,62 @@

from flask import Flask

from extensions.storage.aliyun_storage import AliyunStorage
from extensions.storage.azure_storage import AzureStorage
from extensions.storage.baidu_storage import BaiduStorage
from extensions.storage.google_storage import GoogleStorage
from extensions.storage.huawei_storage import HuaweiStorage
from extensions.storage.local_storage import LocalStorage
from extensions.storage.oci_storage import OCIStorage
from extensions.storage.s3_storage import S3Storage
from extensions.storage.tencent_storage import TencentStorage
from extensions.storage.volcengine_storage import VolcengineStorage
from configs import dify_config
from extensions.storage.base_storage import BaseStorage
from extensions.storage.storage_type import StorageType


class Storage:
def __init__(self):
self.storage_runner = None

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)
elif storage_type == "baidu-obs":
self.storage_runner = BaiduStorage(app=app)
elif storage_type == "volcengine-tos":
self.storage_runner = VolcengineStorage(app=app)
else:
self.storage_runner = LocalStorage(app=app)
storage_factory = self.get_storage_factory(dify_config.STORAGE_TYPE)
self.storage_runner = storage_factory(app=app)

@staticmethod
def get_storage_factory(storage_type: str) -> type[BaseStorage]:
match storage_type:
case StorageType.S3:
from extensions.storage.aws_s3_storage import AwsS3Storage

return AwsS3Storage
case StorageType.AZURE_BLOB:
from extensions.storage.azure_blob_storage import AzureBlobStorage

return AzureBlobStorage
case StorageType.ALIYUN_OSS:
from extensions.storage.aliyun_oss_storage import AliyunOssStorage

return AliyunOssStorage
case StorageType.GOOGLE_STORAGE:
from extensions.storage.google_cloud_storage import GoogleCloudStorage

return GoogleCloudStorage
case StorageType.TENCENT_COS:
from extensions.storage.tencent_cos_storage import TencentCosStorage

return TencentCosStorage
case StorageType.OCI_STORAGE:
from extensions.storage.oracle_oci_storage import OracleOCIStorage

return OracleOCIStorage
case StorageType.HUAWEI_OBS:
from extensions.storage.huawei_obs_storage import HuaweiObsStorage

return HuaweiObsStorage
case StorageType.BAIDU_OBS:
from extensions.storage.baidu_obs_storage import BaiduObsStorage

return BaiduObsStorage
case StorageType.VOLCENGINE_TOS:
from extensions.storage.volcengine_tos_storage import VolcengineTosStorage

return VolcengineTosStorage
case StorageType.LOCAL | _:
from extensions.storage.local_fs_storage import LocalFsStorage

return LocalFsStorage

def save(self, filename, data):
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from extensions.storage.base_storage import BaseStorage


class AliyunStorage(BaseStorage):
"""Implementation for aliyun storage."""
class AliyunOssStorage(BaseStorage):
"""Implementation for Aliyun OSS storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from extensions.storage.base_storage import BaseStorage


class S3Storage(BaseStorage):
"""Implementation for s3 storage."""
class AwsS3Storage(BaseStorage):
"""Implementation for Amazon Web Services S3 storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from extensions.storage.base_storage import BaseStorage


class AzureStorage(BaseStorage):
"""Implementation for azure storage."""
class AzureBlobStorage(BaseStorage):
"""Implementation for Azure Blob storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from extensions.storage.base_storage import BaseStorage


class BaiduStorage(BaseStorage):
"""Implementation for baidu obs storage."""
class BaiduObsStorage(BaseStorage):
"""Implementation for Baidu OBS storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from extensions.storage.base_storage import BaseStorage


class GoogleStorage(BaseStorage):
"""Implementation for google storage."""
class GoogleCloudStorage(BaseStorage):
"""Implementation for Google Cloud storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from extensions.storage.base_storage import BaseStorage


class HuaweiStorage(BaseStorage):
"""Implementation for huawei obs storage."""
class HuaweiObsStorage(BaseStorage):
"""Implementation for Huawei OBS storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from extensions.storage.base_storage import BaseStorage


class LocalStorage(BaseStorage):
"""Implementation for local storage."""
class LocalFsStorage(BaseStorage):
"""Implementation for local filesystem storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from extensions.storage.base_storage import BaseStorage


class OCIStorage(BaseStorage):
class OracleOCIStorage(BaseStorage):
"""Implementation for Oracle OCI storage."""

def __init__(self, app: Flask):
super().__init__(app)
app_config = self.app.config
Expand Down
14 changes: 14 additions & 0 deletions api/extensions/storage/storage_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from enum import Enum


class StorageType(str, Enum):
ALIYUN_OSS = "aliyun-oss"
AZURE_BLOB = "azure-blob"
BAIDU_OBS = "baidu-obs"
GOOGLE_STORAGE = "google-storage"
HUAWEI_OBS = "huawei-obs"
LOCAL = "local"
OCI_STORAGE = "oci-storage"
S3 = "s3"
TENCENT_COS = "tencent-cos"
VOLCENGINE_TOS = "volcengine-tos"
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from extensions.storage.base_storage import BaseStorage


class TencentStorage(BaseStorage):
"""Implementation for tencent cos storage."""
class TencentCosStorage(BaseStorage):
"""Implementation for Tencent Cloud COS storage."""

def __init__(self, app: Flask):
super().__init__(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from extensions.storage.base_storage import BaseStorage


class VolcengineStorage(BaseStorage):
class VolcengineTosStorage(BaseStorage):
"""Implementation for Volcengine TOS storage."""

def __init__(self, app: Flask):
Expand Down
2 changes: 1 addition & 1 deletion api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ authlib = "1.3.1"
azure-ai-inference = "~1.0.0b3"
azure-ai-ml = "~1.20.0"
azure-identity = "1.16.1"
azure-storage-blob = "12.13.0"
beautifulsoup4 = "4.12.2"
boto3 = "1.35.17"
bs4 = "~0.0.1"
Expand Down Expand Up @@ -221,6 +220,7 @@ yfinance = "~0.2.40"
# Required for storage clients
############################################################
[tool.poetry.group.storage.dependencies]
azure-storage-blob = "12.13.0"
bce-python-sdk = "~0.9.23"
cos-python-sdk-v5 = "1.9.30"
esdk-obs-python = "3.24.6.1"
Expand Down

0 comments on commit b360feb

Please sign in to comment.