-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
131 additions
and
2 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
34 changes: 34 additions & 0 deletions
34
api/configs/middleware/storage/volcengine_tos_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,34 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class VolcengineTOSStorageConfig(BaseModel): | ||
""" | ||
Volcengine tos storage configs | ||
""" | ||
|
||
VOLCENGINE_TOS_BUCKET_NAME: Optional[str] = Field( | ||
description="Volcengine TOS Bucket Name", | ||
default=None, | ||
) | ||
|
||
VOLCENGINE_TOS_ACCESS_KEY: Optional[str] = Field( | ||
description="Volcengine TOS Access Key", | ||
default=None, | ||
) | ||
|
||
VOLCENGINE_TOS_SECRET_KEY: Optional[str] = Field( | ||
description="Volcengine TOS Secret Key", | ||
default=None, | ||
) | ||
|
||
VOLCENGINE_TOS_ENDPOINT: Optional[str] = Field( | ||
description="Volcengine TOS Endpoint URL", | ||
default=None, | ||
) | ||
|
||
VOLCENGINE_TOS_REGION: Optional[str] = Field( | ||
description="Volcengine TOS Region", | ||
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,48 @@ | ||
from collections.abc import Generator | ||
|
||
import tos | ||
from flask import Flask | ||
|
||
from extensions.storage.base_storage import BaseStorage | ||
|
||
|
||
class VolcengineStorage(BaseStorage): | ||
"""Implementation for Volcengine TOS storage.""" | ||
|
||
def __init__(self, app: Flask): | ||
super().__init__(app) | ||
app_config = self.app.config | ||
self.bucket_name = app_config.get("VOLCENGINE_TOS_BUCKET_NAME") | ||
self.client = tos.TosClientV2( | ||
ak=app_config.get("VOLCENGINE_TOS_ACCESS_KEY"), | ||
sk=app_config.get("VOLCENGINE_TOS_SECRET_KEY"), | ||
endpoint=app_config.get("VOLCENGINE_TOS_ENDPOINT"), | ||
region=app_config.get("VOLCENGINE_TOS_REGION"), | ||
) | ||
|
||
def save(self, filename, data): | ||
self.client.put_object(bucket=self.bucket_name, key=filename, content=data) | ||
|
||
def load_once(self, filename: str) -> bytes: | ||
data = self.client.get_object(bucket=self.bucket_name, key=filename).read() | ||
return data | ||
|
||
def load_stream(self, filename: str) -> Generator: | ||
def generate(filename: str = filename) -> Generator: | ||
response = self.client.get_object(bucket=self.bucket_name, key=filename) | ||
while chunk := response.read(4096): | ||
yield chunk | ||
|
||
return generate() | ||
|
||
def download(self, filename, target_filepath): | ||
self.client.get_object_to_file(bucket=self.bucket_name, key=filename, file_path=target_filepath) | ||
|
||
def exists(self, filename): | ||
res = self.client.head_object(bucket=self.bucket_name, key=filename) | ||
if res.status_code != 200: | ||
return False | ||
return True | ||
|
||
def delete(self, filename): | ||
self.client.delete_object(bucket=self.bucket_name, key=filename) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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