from fastapi import FastAPI, UploadFile, HTTPException
app = FastAPI()
@app.post("/uploadfile/")asyncdefcreate_upload_file(file: UploadFile = File(...)):# 최대 허용 파일 크기 (500MB)
max_file_size = 500 * 1024 * 1024# 500MB# 파일 크기 확인if file.content_length > max_file_size:
raise HTTPException(status_code=413, detail="File size exceeds the limit of 500MB")
# 여기에서 파일을 처리하거나 저장하는 로직을 추가할 수 있습니다.# 예: file.filename을 사용하여 파일 이름을 얻고, file.file을 사용하여 파일 내용을 처리return {"filename": file.filename}
이 코드에서 file.content_length는 업로드된 파일의 크기를 나타냅니다. 이 값이 max_file_size보다 크면 HTTPException이 발생하고, 클라이언트에게 "File size exceeds the limit of 500MB"라는 메시지가 반환됩니다.