|
|
@@ -12,6 +12,13 @@ from database.config import minio_block,minio_client
|
|
|
from minio import Minio
|
|
|
from pathlib import Path
|
|
|
from sqlmodel import Field, SQLModel,Relationship,Column,Session,select,func,UniqueConstraint,PickleType,text
|
|
|
+import s3fs
|
|
|
+def get_client():
|
|
|
+ s3 = s3fs.S3FileSystem(
|
|
|
+ key='miniokey...',
|
|
|
+ secret='asecretkey...',
|
|
|
+ endpoint_url='https://...'
|
|
|
+ )
|
|
|
|
|
|
class S3Object(SQLModel,table=False):
|
|
|
type:Optional[str]= Field(default=None)
|
|
|
@@ -54,7 +61,7 @@ class S3:
|
|
|
# s3client is minio python SDK client
|
|
|
def __init__(self, bucket='swl', client=None) -> None:
|
|
|
self.bucket = bucket
|
|
|
- self.minio_client = client
|
|
|
+ self.minio_client:Minio = client
|
|
|
# 根据本地时间自动获取对象存储前缀,如 log/2024-04-25/xxx
|
|
|
def get_object_prefix(self, dir='log'):
|
|
|
# 获取时分秒,并且符合路径的格式
|
|
|
@@ -64,7 +71,7 @@ class S3:
|
|
|
# 获取时分秒毫秒,并且符合路径的格式,如 023213_123.json
|
|
|
now = datetime.datetime.now()
|
|
|
# 格式化时间:小时、分钟、秒、毫秒
|
|
|
- formatted_time = now.strftime("%H%M%S_%f") # %f 提供了微秒,所以我们取前三个数字作为毫秒
|
|
|
+ formatted_time = now.strftime("%y%m%d-%H%M%S_%f") # %f 提供了微秒,所以我们取前三个数字作为毫秒
|
|
|
return formatted_time
|
|
|
|
|
|
# def put_dict(self, obj, name_by_time=True):
|
|
|
@@ -100,9 +107,8 @@ class S3:
|
|
|
# 根据文件后缀自动识别 content_type
|
|
|
if content_type == '':
|
|
|
content_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
|
|
|
- object_name = self.get_object_prefix() + '/' + object_name
|
|
|
# 使用 put_object 上传数据
|
|
|
- self.minio_client.fput_object(
|
|
|
+ return self.minio_client.fput_object(
|
|
|
bucket_name=self.bucket,
|
|
|
file_path=file_path,
|
|
|
object_name=object_name,
|