2024-02-09 15:21:33 +08:00
|
|
|
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
|
2024-09-06 14:00:47 +08:00
|
|
|
from extensions.storage.huawei_storage import HuaweiStorage
|
2024-04-29 18:22:03 +08:00
|
|
|
from extensions.storage.local_storage import LocalStorage
|
2024-07-01 17:21:44 +08:00
|
|
|
from extensions.storage.oci_storage import OCIStorage
|
2024-04-29 18:22:03 +08:00
|
|
|
from extensions.storage.s3_storage import S3Storage
|
2024-06-17 19:18:52 +08:00
|
|
|
from extensions.storage.tencent_storage import TencentStorage
|
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):
|
2024-08-15 12:54:05 +08:00
|
|
|
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)
|
2024-09-06 14:00:47 +08:00
|
|
|
elif storage_type == "huawei-obs":
|
|
|
|
self.storage_runner = HuaweiStorage(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-04-29 18:22:03 +08:00
|
|
|
self.storage_runner.save(filename, data)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-11-13 22:05:46 +08:00
|
|
|
def load(self, filename: str, stream: bool = False) -> Union[bytes, Generator]:
|
|
|
|
if stream:
|
|
|
|
return self.load_stream(filename)
|
|
|
|
else:
|
|
|
|
return self.load_once(filename)
|
|
|
|
|
|
|
|
def load_once(self, filename: str) -> bytes:
|
2024-04-29 18:22:03 +08:00
|
|
|
return self.storage_runner.load_once(filename)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-11-13 22:05:46 +08:00
|
|
|
def load_stream(self, filename: str) -> Generator:
|
2024-04-29 18:22:03 +08:00
|
|
|
return self.storage_runner.load_stream(filename)
|
2023-11-13 22:05:46 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
def download(self, filename, target_filepath):
|
2024-04-29 18:22:03 +08:00
|
|
|
self.storage_runner.download(filename, target_filepath)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
def exists(self, filename):
|
2024-04-29 18:22:03 +08:00
|
|
|
return self.storage_runner.exists(filename)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-04-01 20:19:30 +08:00
|
|
|
def delete(self, filename):
|
2024-04-29 18:22:03 +08:00
|
|
|
return self.storage_runner.delete(filename)
|
2024-04-01 20:19:30 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
storage = Storage()
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(app: Flask):
|
|
|
|
storage.init_app(app)
|