FastAPI Tutorial - Uploading File and Downloading File From API Endpoints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so welcome back again my name is Jesse and in this wonderful and exciting tutorial trying to see how to build a very simple application using first API right so this is a simple app that is going to display two concepts how to upload a file to fast API how to upload and download the file right so here yes I come to the first endpoint of this file slash upload I can upload a particular file so let's try it out it's going to give us the option here then if I click on this I can choose a Json file and if I click on the execute is going to retain a violent response for us right so perfect I'm going to retain this valid response 200 okay that is one aspect of it so how do you upload a file to First API second option is you're going to also upload a file right so we can reset it then let's upload the file so try it out and then we are going to do something with the file and then return the option of downloading that particular result right so if I upload a Json file click on execute is going to run it for us if I come down here it has executed perfectly for us we have the option of downloading a file right if I click on this you can see that just converted it into a valid Json file perfect hey follow the ml4 so we are converting Json to yaml just to demonstrate the point of how to upload and how to download with fast API okay so let's see how to do that so I'll just go back again to my workspace that is the one I just run it come back here in case you don't have any of these things let's go create a virtual environment so python basically very simple in the comment Dash m VNV right and I'm going to create EV so yeah because I have a lot of python versions of my system let's create a very simple one here perfect and then if I go into this here I check it out we have my update Piper let's remove this one from the previous one that we had and then let's activate this environmental source pnv script or bin activate then let's install the beauty package so be installing these steps so in case you don't have any of them you just have to install fast API uvicon python ml python multiple multi-part YouTube for the download right for the download feature and you can also install yaml so we also have I am here let's go to install the login for us Json is going to come by default perfect and now since it has been installed we're just going to create a file for apps to touch up dot pi and then let's open it with vs code you can also use Sublime if you want now if I come back here we can now start from scratch so I'm just going to copy the basic stuff so yeah yes we don't need this so we are just going to import these packages so first API right and then for first day we are going to import file or upload file the difference between this is a this is stored at a sample temporal file so this will be using and I'm going to import uvcon which is going to be used to run our app it's going to initialize our app here to create our first endpoint you're going to save our app so these are basic stuff right so if I say this one and I run it inside my thumbnail so a new terminal which is here so yes let's close this from here right this is a place we are on it right so we have activated aperture environment and now let's see how to work on this right so the simplest approach if in case I want to run it I'll just go with my UV cone write my app which is referring to the name of the app here and then my app that I'm running here right this is this initialize one so the app this first one is referring to the file name and the second one which is this option here this option here refers to the app instance right you are calling this particular distance which have been initialized then let's go with dash dash reload right to continue reloading it so selling that it's already in use because the previous one is still running so let's drop this one and then let's go back again and then let's run it again so I rerun it again the Facebook is running if I go back and I check it out refresh it so that we already have only one endpoint right which is a previous one if I click on it we can try it out and we get our result back right so how do you add a file upload so we go back again and you're going to create another endpoint for that which is going to be my ADD sign right so just create my heart let's go just post because you are posting a file and I'm going to specify the end point all right so the end point is going to be the endpoint for that so let's call it as file slash upload that is the endpoint you're going to be going to then let's create our file selected test file or upload file if I want to display the basic stuff right they need to specify the name of the file the valve we are taking and this variable takes the type of uploaded file or upload file this upload file class or objects and is coming from here right from first API is very very important then from there I can now read the particular file right so in case I want to read it I can just go with my file so let's go just yeah you can call this file right oh and give it as the data because I want to read it it's going to be this option here right which is going to be this option here right so this is the data I want to read so in case I want to read it so file.file right then Dot read so this option will allow it to be red right file.file.read it's going to be are going to allow it to be red if I go with file.file right then in case I want to get the file name I can just go back again to this object so let's say I want to return the content of it going to be this option here so this is going to be let's say yeah content of that particular file because I want to get a content and I want to get the particular file name to file name so lucky it's going to be the file dot file right file name so this is going to give us the name of the file right very simple in case I want to get the data that was read I can just use any file so in this case we are using Json to help us with that so I can just import Json from the top of the file here so let's import Json that's what you want to read which is better and then in case I want to read I can just go with Json dot load right so we have load if I go with load then that means I don't need to go with breed right but if I go with loads then I have to go with this option Dot read right so that I'll be able to read it so s yeah times have to read it well then I can now retain this particular data so let's save it everything is working now so I go back to my endpoint and then I refresh it again we have the first endpoint which is the one that we had here and we have the second one forward slash upload if I click on this one here we have this option here so if I click on the try we have this option the file browser option that we can actually upload for apologies so in case I click on this one here and then it can execute hopefully there's no error perfect so that's just given us the content of it right so this is the name of the file so example.json which is the same name that was supplied here right and then we also have the content read for us is a basic understanding behind what you have done so far right so this is one of the ways of reading a file so you have seen how to upload a file you can also add validation check to it so let's say if the file dot contents right type you can select either content type is not equal to let's say it's not equal to the specified application slash Json right then I want you to raise an arrow so there is an http exception Arrow so yeah so this HTTP exception Arrow will come from fast API so I have to just go back here so from first API dot exceptions import the particular stuff I want to do I think it's exceptions input the HTTP stuff right so 80tp I don't know how http deception right it's not exceptional it's exception right and then that is all so this then I'm going to pass in my what I want to the particular so zero or like 400 then I'm going to give a detailed message because most people don't know what it means and I'm going to tell us okay this is a invalid document type so that is how to check so either type of this file is not Json I want you to retain this Arrow right else that is if everything is correct then I want you to read a file and then return the result perfectly so if I save it here I go back hopefully there's no error now and let's go back to what we have try it out let's pick a file but it's valid execute e2x let's pick another file that is not valid which is let's say I want to pick this TST file open execute and let's check it out so invalid document type because this is not a valid Json file right okay so that is how to check for the file type exception so that is one way so we have seen how to upload a file and then read the result of it that is one of the options the next option is I want to upload it do something to the file that I've uploaded and then create a download option so how do we do that so this is going to be the test option so this is going to be upload and then let's see download for something like that so you want to upload the file and download it and this is also going to take the file option and then upload class right from Fast API perfect so let's give it a description so what this does is that it's going to return a Gmail file for the uploaded his own file so yeah uploading the Json file then you are returning a yaml file so let's give the endpoints for that one so at up Dot dot post then I'm going to pass in my endpoint which is going to be my file let's call this upload and download yeah it's not a good name but you keep it like that that's the endpoint you're creating and then let's work on our first stop so just as we did above here in which you ready for and we uploaded it we read the file so we just copy the same thing here we get it to you right so if it's not this keyword says there else read that file now we want to be able to write it to let's say yaml right so that is where the adoption so let's give it the file name so I want to write it to yaml so I'm going to give it there are two things we want to do first we want to so there are two things you want to do let's call this one as Json data so there are two things you want to do first we want to save the file so we have read a file we want to write the file to a file right and then also need a place to store it store the saved file right and then from there we can now retain that one as a download file to this first step you have read it the second step you are going to write a particular data that you have right to a format of let's say a yaml file and I'm going to store that yaml file and retain that file as download so let's see some stuff so since we are supposed to save or store the seed file we need to give it a particular place we want to save it so I'll go back to the top of my file here I'm going to create a bit directory so base we want directory which is going to be my os.parts DOT directory name die Dr name right then I'm also going to create OS dot part dot absolute part we want we want to use an absolute part so up path for that particular current file so this is useful in case you want to deploy so that it's always going to pick it up so we need to import OS we will format it later with black pivot so it is going to be our B directory I also need to create our media all right we will be saving the entire stuff all you can call it as upload territory right where we'll be saving the stuff so where's that path to join then I'm going to combine the ps33 to another upload right and that's where we'll be saving all the files right so how to create this particular directory pivot so we have created these things here so this is where we'll be storing our stuff now let's go back here and let's specify the place you'll be saving our part so let's receive Parts yeah save fifth file path right which in this case is going to the reason you're doing that you want to save it in the place and download it right you can't just download it straight away you can but this is a simple approach so before we do that we want to create a new file name so new funny and then we'll be using a very simple approach to get it to where that part but split EST we splitting the extension from the file dot file name right now that we have this file name here this option here I'm going to split it then when you split it we want to get the first the last values so the the file itself without the extension what do I mean by that so we want to get whatever file we uploaded purchase I'm going to get this here without the extension understanding perfect and then this is going to be the file name right so let's use some if you have any question or contribution complete the comment section below then I want to add a timestamp because you are storing it inside the same location you don't want to override other people's work through dot formats then we are passing the current file that we have and then the time stamp the timestamp is coming from here which is this important time then you're creating a timestamp perfect and now we can just go without yaml so this goes with the new founding now I'm going to save it to this particular file part it's going to be OS dot part dot join you can also use part lip in case you want and I'm going to pass in my uploads directory and then my file name identifying perfect that is a bit tough so this we are going to store it now let's write it right so we want to write it somewhere and save it to this particular power so so we're going to write this with open I'm going to open the save file part and so we are saving it to open it as read right if then we want to jump you want to use the ammo so yaml dot dump I'm going to pass in the file name itself which is this particular part that you have created and then we want to dump into it this particular Json file so Json data into this particular file right so we have to import demo from up from above import demo a button if you are loading or we are damping the content of it so we are opening the file we are dumping onto this particular IMO file that we have created here this new file name which has been saved here their particular data that we are floated from above perfect now let's retain the results we retain as a download file so we turn it so let me copy this one from here to retain and how do we return it to return it to the file response option so we turn it as a file response option now to go to the top of my file and import the data transfer from first API Dot responses let's import file response right so that is what to be using to do the response to file response then this one takes in the path with the part that they follow see that which in our case is this C4 power right that's why we had it there and then it takes in the particular application type that you are sending right since we are selling a yaml file just give it a particular application type right we're giving it the media type or for this type right so this is going to be media then type then from there I'll just pass in my media type for this application slash October octet stream is every file it's very powerful so in keep doing it the file format to just use this okay and then we're going to go with the file part right or the file name so if you go into that particular directory and then it's going to pick the new phone if we did not save four parts there they are a lot of faster but it's going to pick this particular file that you are sending and then when it returns it's going to return to the right funny right so let's check it and see if it's working first so go back restart it again so we have this endpoint if I click on try it out and I select my ammo file or my Json file execute hopefully it doesn't give us an arrow give us an internal server error so let's check the reason why for we have that Arrow you could just go back again here it's in a new search for phone right everything that we don't have this directory the directory here which is this directory uploads its new model so we have to go back and then create it so this is the previous one that we had here and then let's create that directory so the name of the directory is uploads right and it must correspond to his uploads and now if I save it everything is working as expected let's go back here clear everything off choose a file let's choose example as you can see it's uploaded and if I click on execute it doesn't give us any error 200 and then now I can download a particular file if I go back to this directory you can say a file is saved there and I can see the content of it right so that is how to use fast API for uploading a file and then downloading a particular file so the first approach is that we saw how to upload the file right that is a basic option the most important thing is important upload file from first API here itself and then we also check the type so that we know that we are accepting only Json because it's important you don't want anybody to upload into your system and then we write it out right so in case you want to read it just go with this option here and we could get a file name from from this and in case also going to upload a file do something with it and then download it we have this option here the same option but for the downloading you need to get the part that you'll be saving the file to right after modifying it in another writing everything you have to use the file response to retain the parts of where the file has received the media type right and then the particular file name right that is going to be used when you download it that is a basic overview of how to use fast API in case you want to upload a file and increase you want to upload and download the file from Fast API so thank you for watching the tutorial hope you have learned something see you another time stay blessed bye
Info
Channel: JCharisTech
Views: 21,412
Rating: undefined out of 5
Keywords: fastapi tutorial, uploading files in fastapi, downloading files from fastapi, API, Python, How to upload a file to an API Endpoint, JCharisTech, REST API, flask, django, django-restframework, Jesse E.Agbe(JCharis), Jesus Saves, FileResponse in FastAPI
Id: m6Ma6B6VlFs
Channel Id: undefined
Length: 23min 51sec (1431 seconds)
Published: Sun Dec 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.