feat: api get_presign_url
continuous-integration/drone/push Build is passing Details

This commit is contained in:
BryantHe 2023-08-16 17:13:52 +08:00
parent 916f119d50
commit 95079adc6b
3 changed files with 31 additions and 6 deletions

View File

@ -7,8 +7,8 @@ from apiflask import APIBlueprint
from flask import session from flask import session
from app import rpc from app import rpc
from app.api.v1.exception.api import ImageUploadError, UserInfoError from app.api.v1.exception.api import ImageUploadError, UserInfoError, ImageNotFound
from app.api.v1.schema.api import ImageIn from app.api.v1.schema.api import ImageIn, ImagePreSignUrlOut, ImagePreSignUrlIn
from app.util.auth import login_required from app.util.auth import login_required
api = APIBlueprint('api', __name__) api = APIBlueprint('api', __name__)
@ -18,7 +18,7 @@ api = APIBlueprint('api', __name__)
@api.doc(summary='上传图片', description='上传图片') @api.doc(summary='上传图片', description='上传图片')
@api.input(ImageIn, location='files') @api.input(ImageIn, location='files')
@login_required @login_required
def wechat_login(files_data): def upload_image(files_data):
f = files_data['image'] f = files_data['image']
try: try:
result = rpc.storage.upload(file_name=str(session['user_id']+f'_{f.filename}'), result = rpc.storage.upload(file_name=str(session['user_id']+f'_{f.filename}'),
@ -41,6 +41,21 @@ def wechat_login(files_data):
return {'msg': 'uploading image fail'} return {'msg': 'uploading image fail'}
@api.get('/images/presign_url')
@api.input(ImagePreSignUrlIn, location='query')
@api.output(ImagePreSignUrlOut)
@api.doc(summary='获取图片预签名链接', description='获取图片预签名链接')
def get_image_presign_url(query_data):
try:
result = rpc.storage.get_presign_url(query_data['avatar_id'],
'bodyrecord',
bucket='bodyrecord',
expire_time=query_data.get('expire_time', 3600))
except Exception as e:
raise ImageNotFound(extra_data={'error_docs': str(e)})
return result
@api.get('/hello') @api.get('/hello')
def hello(): def hello():
result_one = f'我是您的专属接口提供服务器:' result_one = f'我是您的专属接口提供服务器:'

View File

@ -9,3 +9,8 @@ class ImageUploadError(HTTPError):
class UserInfoError(HTTPError): class UserInfoError(HTTPError):
status_code = 500 status_code = 500
message = '设置用户信息失败' message = '设置用户信息失败'
class ImageNotFound(HTTPError):
status_code = 404
message = '获取图片链接失败'

View File

@ -1,12 +1,17 @@
from apiflask import Schema from apiflask import Schema
from apiflask.fields import File from apiflask.fields import File, URL, String, Integer
class ImageIn(Schema): class ImageIn(Schema):
image = File() image = File()
class FileOut(Schema): class ImagePreSignUrlIn(Schema):
file = File() avatar_id = String(required=True)
expire_time = Integer()
class ImagePreSignUrlOut(Schema):
image_url = URL()