test3.py 912 B

1234567891011121314151617181920
  1. from fastapi import APIRouter, Depends, File, UploadFile
  2. from api.common.logger import logger
  3. router = APIRouter()
  4. # @router.post("/upload/file/", summary="上传图片")
  5. # async def upload_image( file: UploadFile = File(...) ):
  6. # logger.info(f"用户{token_data.sub}->上传文件:{file.filename}")
  7. # # 本地存储临时方案,一般生产都是使用第三方云存储OSS(如七牛云, 阿里云)
  8. # save_dir = f"{settings.BASE_DIR}/assets"
  9. # if not os.path.exists(save_dir):
  10. # os.mkdir(save_dir)
  11. # try: suffix = Path(file.filename).suffix
  12. # with NamedTemporaryFile(delete=False, suffix=suffix, dir=save_dir) as tmp:
  13. # shutil.copyfileobj(file.file, tmp)
  14. # tmp_file_name = Path(tmp.name).name
  15. # finally:
  16. # file.file.close()
  17. #
  18. # return response_code.resp_200(data={"image": f"http://127.0.0.1:8010/assets/{tmp_file_name}"})