update: 对阿里云OSS的私有Bucket支持访问
This commit is contained in:
@@ -133,7 +133,26 @@ async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession =
|
||||
'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除',
|
||||
'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code}
|
||||
}
|
||||
elif settings.STORAGE_ENGINE == 'aliyunsystem':
|
||||
info.text = await storage.get_filepath(info.text)
|
||||
return {
|
||||
'detail': f'取件成功,链接将在{settings.ACCESSTIME}秒后失效',
|
||||
'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code}
|
||||
}
|
||||
|
||||
@app.post('/adminDownloadFile',dependencies=[Depends(admin_required)], description='管理员获取资源链接')
|
||||
async def admindownloadfile(filetext: str, s: AsyncSession = Depends(get_session)):
|
||||
if storage.STORAGE_ENGINE == 'aliyunsystem':
|
||||
filetext = await storage.get_filepath(filetext)
|
||||
return {
|
||||
'detail': f'获取文件链接成功,链接将在{settings.ACCESSTIME}秒后失效',
|
||||
'fileURL': filetext
|
||||
}
|
||||
elif storage.STORAGE_ENGINE == 'filesystem':
|
||||
return {
|
||||
'detail': f'获取文件链接成功',
|
||||
'fileURL':filetext
|
||||
}
|
||||
|
||||
@app.get('/banner')
|
||||
async def banner(request: Request, s: AsyncSession = Depends(get_session)):
|
||||
@@ -176,11 +195,11 @@ async def get_file(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession
|
||||
return {'detail': '查询成功', 'data': info.text}
|
||||
# 如果是文件,返回文件
|
||||
else:
|
||||
if settings.STORAGE_ENGINE == 'filesystem':
|
||||
filepath = await storage.get_filepath(info.text)
|
||||
if settings.STORAGE_ENGINE == 'filesystem':
|
||||
return FileResponse(filepath, filename=info.name)
|
||||
else:
|
||||
return {'detail': '查询成功', 'data': info.text}
|
||||
return {'detail': '查询成功', 'data': filepath}
|
||||
|
||||
|
||||
@app.post('/share', dependencies=[Depends(admin_required)], description='分享文件')
|
||||
|
||||
@@ -59,3 +59,5 @@ KeySecret = config('KeySecret',cast=str,default="阿里云账号AccessKeySecret"
|
||||
OSS_ENDPOINT = config('BUCKET_URL', cast=str, default="阿里云OSS Bucket的地域节点")
|
||||
# 阿里云OSS Bucket的BucketName
|
||||
BUCKET_NAME = config('BUCKET_NAME', cast=str, default="阿里云OSS Bucket的BucketName")
|
||||
# 访问文件的读取时长(s)
|
||||
ACCESSTIME = config('ACCESSTIME', cast=int, default=60)
|
||||
|
||||
+10
-5
@@ -9,8 +9,9 @@ import oss2
|
||||
|
||||
|
||||
class AliyunFileStore:
|
||||
def __init__(self):
|
||||
auth = oss2.Auth(settings.KeyId, settings.KeySecret)
|
||||
bucket = oss2.Bucket(auth, settings.OSS_ENDPOINT, settings.BUCKET_NAME)
|
||||
self.bucket = oss2.Bucket(auth, settings.OSS_ENDPOINT, settings.BUCKET_NAME)
|
||||
|
||||
def upload_file(self, upload_filepath, remote_filepath):
|
||||
self.bucket.put_object_from_file(remote_filepath, upload_filepath)
|
||||
@@ -23,7 +24,9 @@ class AliyunFileStore:
|
||||
return text
|
||||
|
||||
async def get_filepath(self, text: str):
|
||||
return text
|
||||
text = text.strip(f"https://{settings.BUCKET_NAME}.{settings.OSS_ENDPOINT}/")
|
||||
url = self.bucket.sign_url('GET', text, settings.ACCESSTIME, slash_safe=True)
|
||||
return url
|
||||
|
||||
@staticmethod
|
||||
async def get_size(file: UploadFile):
|
||||
@@ -54,13 +57,15 @@ class AliyunFileStore:
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
async def delete_file(self, text: str):
|
||||
text = text.strip(f"https://{settings.BUCKET_NAME}.{settings.OSS_ENDPOINT}/")
|
||||
self.bucket.delete_object(text)
|
||||
|
||||
|
||||
class FileSystemStorage:
|
||||
DATA_ROOT = Path(settings.DATA_ROOT)
|
||||
STATIC_URL = settings.STATIC_URL
|
||||
NAME = "filesystem"
|
||||
def __init__(self):
|
||||
self.DATA_ROOT = Path(settings.DATA_ROOT)
|
||||
self.STATIC_URL = settings.STATIC_URL
|
||||
self.NAME = "filesystem"
|
||||
|
||||
async def get_filepath(self, text: str):
|
||||
return self.DATA_ROOT / text.lstrip(self.STATIC_URL + '/')
|
||||
|
||||
Reference in New Issue
Block a user