How to create server of files with FastAPI

In this example I will show you how to upload, download, delete and obtain files with FastAPI

How to upload files by Form Data using FastAPI

In the following code we define the file field, it is there where we will receive the file by Form …


This content originally appeared on DEV Community and was authored by Nelson Adonis Hernandez

In this example I will show you how to upload, download, delete and obtain files with FastAPI

How to upload files by Form Data using FastAPI

In the following code we define the file field, it is there where we will receive the file by Form Data

from fastapi import FastAPI, UploadFile, File
app = FastAPI()

@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
    with open(file.filename, 'wb') as image:
        content = await file.read()
        image.write(content)
        image.close()
    return JSONResponse(content={"filename": file.filename},
status_code=200)

Upload files to fastapi with postman

How to download files using FastAPI

from fastapi import FastAPI
from os import getcwd
from fastapi.responses import FileResponse
app = FastAPI()

@router.get("/download/{name_file}")
def download_file(name_file: str):
    return FileResponse(path=getcwd() + "/" + name_file, media_type='application/octet-stream', filename=name_file)

How to get files using FastAPI

from fastapi import FastAPI
from os import getcwd
from fastapi.responses import FileResponse

app = FastAPI()

@router.get("/file/{name_file}")
def get_file(name_file: str):
    return FileResponse(path=getcwd() + "/" + name_file)

How to delete files using FastAPI

from fastapi import FastAPI
from os import getcwd, remove
from fastapi.responses import JSONResponse

app = FastAPI()

@router.delete("/delete/file/{name_file}")
def delete_file(name_file: str):
    try:
        remove(getcwd() + "/" + name_file)
        return JSONResponse(content={
            "removed": True
            }, status_code=200)   
    except FileNotFoundError:
        return JSONResponse(content={
            "removed": False,
            "error_message": "File not found"
        }, status_code=404)


This content originally appeared on DEV Community and was authored by Nelson Adonis Hernandez


Print Share Comment Cite Upload Translate Updates
APA

Nelson Adonis Hernandez | Sciencx (2021-07-30T16:09:04+00:00) How to create server of files with FastAPI. Retrieved from https://www.scien.cx/2021/07/30/how-to-create-server-of-files-with-fastapi/

MLA
" » How to create server of files with FastAPI." Nelson Adonis Hernandez | Sciencx - Friday July 30, 2021, https://www.scien.cx/2021/07/30/how-to-create-server-of-files-with-fastapi/
HARVARD
Nelson Adonis Hernandez | Sciencx Friday July 30, 2021 » How to create server of files with FastAPI., viewed ,<https://www.scien.cx/2021/07/30/how-to-create-server-of-files-with-fastapi/>
VANCOUVER
Nelson Adonis Hernandez | Sciencx - » How to create server of files with FastAPI. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/30/how-to-create-server-of-files-with-fastapi/
CHICAGO
" » How to create server of files with FastAPI." Nelson Adonis Hernandez | Sciencx - Accessed . https://www.scien.cx/2021/07/30/how-to-create-server-of-files-with-fastapi/
IEEE
" » How to create server of files with FastAPI." Nelson Adonis Hernandez | Sciencx [Online]. Available: https://www.scien.cx/2021/07/30/how-to-create-server-of-files-with-fastapi/. [Accessed: ]
rf:citation
» How to create server of files with FastAPI | Nelson Adonis Hernandez | Sciencx | https://www.scien.cx/2021/07/30/how-to-create-server-of-files-with-fastapi/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.