Fast API Tutorial, Part 17: Request Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome to part 17 of our fast api tutorial in this video we are going to talk about request files so let's just go ahead and get right into it we've got our app running everything is the same as it always is well not always first couple videos were different but you know i'm i'm learning i'm getting there cut me a little slack okay app.post and we will just say files we'll create the the operation create file file and we're going to import this as a bytes object return file and let's go ahead and see what happens let's try this out i've got this little file right here i'm going to show it to you i've got it in here testing file to be uploaded that's all it says just a simple text file we hit execute and nothing happens what we need to do is we need to tell fast api that this is going to be a file object while we're doing this i'm just going to go ahead and create another route upload file async def create upload file file and this one is going to be an upload file and we'll call this file.file name and we'll make this the length of the file okay now you can see we need to import both of these so we go up here and we import file upload file save it and now let's come back over here and now we try this again to be uploaded execute and we get a length of 27. great we try it in here let's try this out to be uploaded and we get the file name is to be uploaded.txt okay so we needed to actually declare this as a file type because this is just the bytes this doesn't tell fast api anything of of what we're actually including here we had to actually include we had to add this file attribute in order to be able to handle that okay now there are um so you can obviously upload the file in in in both of these ways um we're just gonna say here let's instead let's debug and we'll upload this file and we execute and let's just take a look at this file this isn't really anything this is just a bytes object with this string right here we go further and this works let's now instead take a look at what we have with the upload file execute so you can see this it inherits from starlet it's a specific data structure and we have all this information here we have the spooled temporary file we have the content type we have the file name we have the max size um and you notice we didn't have to actually tell it that it was going to be a file type we could just include this right here um those are i mean a few of the reasons why it's it's probably better if you're going to upload a file to use the upload file type you have the the benefit of a spooled file is that it's going to be stored in memory up to a specific size which we saw right here spool max size is 104 8576 um after that is going to be stored in the disk but we get just it's it's actually fast api knows that it's a file it's not just assuming that it's a bytes object that we're telling it's a file type okay so you generally want to upload it using the upload file type another benefit to using an upload file type as opposed to the bytes we can do something like contents equals await file.read so now let's go ahead and do this again refresh create upload file try it out to be uploaded so you can see now we get our contents here it has you know you can write to certain things you there there are a myriad of different things that you can do if you go to [Music] starlet and if we look at spooled nope not here we need to go to github you can see there's stuff about the spool temporary file there's the data structure here so you can look in to see some of the stuff that that you get from using this type in there oh let's go ahead and end that process okay the next thing that we need to notice is that like we've seen before we are using form data to upload the file okay we will see in just a little bit about how we can how we can do that um just in a regular template excuse me um but when we include a file uh fast api knows to use the multi-part form data instead of the x www forum url encoded that that sort of information okay another note we can't you you have to use um file and form parameters together but you can't also then declare a body field we saw that in the last video where um if you include a a a form attribute and a body attribute then it's going to convert it all to the form data instead of the json it kind of just takes control as form data okay let's go ahead and make this optional and see what happens so let's make this bytes or none and make this none if not file return message no file sent and let's do the same thing over here upload file or none equals none if not file return message no upload file sent otherwise we're going to return what we had before and i will take this off debug mode because i don't want it to stop each time i go through and we'll refresh the page try it out execute no file sent we have that checked we can do the same thing down here try it out execute no upload file send okay um kind of as we've seen before uh with body and field and all those other parameters we can add in extra information description equals a file read as bytes and here we can make this let's do let's make this required equals file um description equals a file read as upload file if i can type ctrl s refresh our page and you can see we have a nice little description here there's a whole bunch of other metadata that you can include um you know we've seen stuff like that before so i'm not really going to touch on that too much last thing i'm going to mention is the ability to add in multiple files so if we want to make this a list of bytes and let's make this required just just because and then here we will do length we're going to return length of let's call this files plural file sizes is going to be length of file for file in files and we'll do the same thing down here list of upload file and i can get rid of that we will put that we'll use a list comprehension for file in files okay it's going to be the same general process we go in here and we can try it out choose file let's see if this this works no it just added to one of them okay whatever requirements execute file sizes 27 and 61. we do the same thing down here try it out requirements and to be uploaded execute requirements to be uploaded okay fairly simple um we can add in uh just for fun if we really want to um i don't know if i want to do it you know i'll just i'll just do it i'm going to throw in a little bit of um a little bit of just html async def main content equals and i'm going to copy paste from what sebastian set up okay and we will return html response content equals content so now we go back in here we refresh main you notice we don't really get i mean we get this but this doesn't help us what we want to do is we want to do localhost 8000 and here you can see we have a nice little form input multi-part form data method is post you can see we add in this attribute here of including multiple files and we can do it just like we did before we can do this one if we want either one oh oh that's right it's because i'm not actually going to it okay or it's i'm not actually there's no route that i'm redirected to is what i should say if we go in here um let's go here choose files open submit you can see we got the 200 response we got this okay that's about it for file uploads um that's there's a lot of just general stuff that we've done so far in terms of um uh requests and response and stuff like that um starting i don't know about the next video next video is gonna be request forms and files so we're gonna kind of you know combine some of the functionality that we've seen but then we're going to get into error handling path operation configuration um encoders stuff like that some more the the nitty gritty the the technical stuff that goes behind the scenes as opposed to just um just like request response cycle sort of stuff okay um this video ran a little bit longer than i anticipated not as long as some other ones i'm getting better at that but i will see you in the next video where we tackle request forms and files
Info
Channel: JVP Design
Views: 5,554
Rating: undefined out of 5
Keywords: fastapi
Id: _XgKod1fqzo
Channel Id: undefined
Length: 13min 39sec (819 seconds)
Published: Tue Jun 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.