Request Files - FastAPI

PHOTO EMBED

Wed Apr 20 2022 05:13:44 GMT+0000 (Coordinated Universal Time)

Saved by @newaaa41 ##python ##fastapi ##file ##fileupload

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/files/")
async def create_file(file: bytes = File(...)):
    return {"file_size": len(file)}


@app.post("/uploadfile/")

async def create_upload_file(file: UploadFile):

    return {"filename": file.filename}
content_copyCOPY

Using UploadFile has several advantages over bytes: You don't have to use File() in the default value of the parameter. It uses a "spooled" file: A file stored in memory up to a maximum size limit, and after passing this limit it will be stored in disk. This means that it will work well for large files like images, videos, large binaries, etc. without consuming all the memory. You can get metadata from the uploaded file. It has a file-like async interface. It exposes an actual Python SpooledTemporaryFile object that you can pass directly to other libraries that expect a file-like object

https://fastapi.tiangolo.com/tutorial/request-files/