forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support huawei cloud obs storage (langgenius#7980) (langgenius#7981)
- Loading branch information
Showing
9 changed files
with
1,067 additions
and
949 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
api/configs/middleware/storage/huawei_obs_storage_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class HuaweiCloudOBSStorageConfig(BaseModel): | ||
""" | ||
Huawei Cloud OBS storage configs | ||
""" | ||
|
||
HUAWEI_OBS_BUCKET_NAME: Optional[str] = Field( | ||
description="Huawei Cloud OBS bucket name", | ||
default=None, | ||
) | ||
|
||
HUAWEI_OBS_ACCESS_KEY: Optional[str] = Field( | ||
description="Huawei Cloud OBS Access key", | ||
default=None, | ||
) | ||
|
||
HUAWEI_OBS_SECRET_KEY: Optional[str] = Field( | ||
description="Huawei Cloud OBS Secret key", | ||
default=None, | ||
) | ||
|
||
HUAWEI_OBS_SERVER: Optional[str] = Field( | ||
description="Huawei Cloud OBS server URL", | ||
default=None, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from collections.abc import Generator | ||
|
||
from flask import Flask | ||
from obs import ObsClient | ||
|
||
from extensions.storage.base_storage import BaseStorage | ||
|
||
|
||
class HuaweiStorage(BaseStorage): | ||
"""Implementation for huawei obs storage.""" | ||
|
||
def __init__(self, app: Flask): | ||
super().__init__(app) | ||
app_config = self.app.config | ||
self.bucket_name = app_config.get("HUAWEI_OBS_BUCKET_NAME") | ||
self.client = ObsClient( | ||
access_key_id=app_config.get("HUAWEI_OBS_ACCESS_KEY"), | ||
secret_access_key=app_config.get("HUAWEI_OBS_SECRET_KEY"), | ||
server=app_config.get("HUAWEI_OBS_SERVER"), | ||
) | ||
|
||
def save(self, filename, data): | ||
self.client.putObject(bucketName=self.bucket_name, objectKey=filename, content=data) | ||
|
||
def load_once(self, filename: str) -> bytes: | ||
data = self.client.getObject(bucketName=self.bucket_name, objectKey=filename)["body"].response.read() | ||
return data | ||
|
||
def load_stream(self, filename: str) -> Generator: | ||
def generate(filename: str = filename) -> Generator: | ||
response = self.client.getObject(bucketName=self.bucket_name, objectKey=filename)["body"].response | ||
yield from response.read(4096) | ||
|
||
return generate() | ||
|
||
def download(self, filename, target_filepath): | ||
self.client.getObject(bucketName=self.bucket_name, objectKey=filename, downloadPath=target_filepath) | ||
|
||
def exists(self, filename): | ||
res = self._get_meta(filename) | ||
if res is None: | ||
return False | ||
return True | ||
|
||
def delete(self, filename): | ||
self.client.deleteObject(bucketName=self.bucket_name, objectKey=filename) | ||
|
||
def _get_meta(self, filename): | ||
res = self.client.getObjectMetadata(bucketName=self.bucket_name, objectKey=filename) | ||
if res.status < 300: | ||
return res | ||
else: | ||
return None |
Oops, something went wrong.