Python Upload Files To SharePoint Using Office365 Rest Package Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what up god what up what up i am blue in this video guys we're going to be talking about uploading files to sharepoint so this i'm doing a multi-part series and we're going to be adding code to existing code that we already you know started um so the previous video that i made which is part one it was to download files so in this video i want to show you how to upload files and we're going to modify the code and i'll be pushing update changes to my um github account as well so you can take a look at that but that's kind of what we're going to be doing here we're adding on to it right so we're going to be uploading but we need to add a function first to our sharepoint class that we started in the previous part one take a look at that if you want to kind of you know go through that on how that works um so we're gonna do that first and then we're gonna create a upload files py file just kind of show you put some examples of you know how to upload files guys so before we get started if you give me a follow give me a like appreciate it guys hit that follow hit that like guys so one of my sponsors for the video is go share it dot io take a look at it they're a sharing platform um ultimately their app is designed to share files large and small file doesn't matter and um just makes it easier for the recipient where all they need is the download link and if it's password protected you know longer they have the password or the passcode they could have access to the file to download and again it's very ideal for large files as well you know multi-gig size files so go take a look at it you know they're sponsoring um this video and um yeah go sign up and um let me know what you think all right guys so now let's get started on our video all right guys so we're gonna start the again those y'all who are just starting we go take a look at part one if you want to get familiar on this office 365 api file but ultimately we're using the office 365 package um and then of course from there we're kind of some simplifying the the um authentication you know and ultimately um you know creating some functions to get you know download files to get file lists so on and so on so what though what one of the things that we're going to do here since we're trying to upload files we're going to end up creating a upload files upload file function so let's go ahead and let's do that so in this case it would be called upload file and this is going to take uh three arguments right so one is going to be file name uh the second one is going to be folder name which is the sharepoint folder with sub dom names as well if you wanted to go to a subfolder i don't know i don't know why i said subdomain sub domain i meant to say subfolder then the third one is going to be the context of the file right we got to read the file so whatever context belongs to that file whether it be you know uh text you know binary doesn't matter whatever that is that's going to get uploaded as well all right so those are three arguments that we need so now let's go ahead and start doing the first thing we need to do is get our connection we're going to create a connection in this case would be the underscore als function which is again up here to authenticate as you can tell it's already passing the credentials to authenticate um the second uh the next thing i'm gonna do we need to build our target um folder url all right so in this case it's going to be sites forward slash sharepoint uh site name right the name of our sharepoint site um forward slash the next one is is sharepoint docs again same thing forward slash and then it's going to be the folder name right the sharepoint uh this is gonna be a a argument that we're gonna pass in you know and i'll show you an example of how that looks and everything too so that's all we don't have to specify the a file name or nothing here we're just specifying the pretty much the target which folder we want to upload the file to that's kind of what we're doing here uh the next thing is going to be we're going to call this target folder and this would be connect web get folder by bypass right so that's what we're going to specify here it's going to be bypass then our argument inside here would be our target folder url right so pretty much we're specifying the the uh the pass of which folder we want to you know save it to and that's kind of what we're doing here all right so once we do that the next thing is let me create an object we're going to call this uh upload status because it's going to be the results right the response i guess i can just call it response so it'll be response and this would be target folder upload file and you can tell the first argument is file name so i'm going to specify file name and then it'll be our context so i'm going to specify our context right which is this um argument that's coming into this function and the file name they're going to get passed in right so that's kind of what we're doing here and then on top of that we got to do a execute query and that's it we're going to get a response back and then i could go ahead and return that response um yep that's it return the response so from up from a response this is a normal http response so i mean you could either get the contacts um the the status code if you want to add some other logic where if it returns a 400 status code or 403 or whatever maybe you could do some other stuff with it we're not going to go that detail that's that's on a you know that's on a per use case on what you're trying to do right but either way it's just a normal http response is what this is so this is it man this is the only thing that we need to add to our our office 365 api file we're adding the upload file function so now let's kind of put an example together and like how to use it i'm gonna go and then create a new file and we're gonna call this new file let's call it upload uh files dot py right so that's kind of what we're gonna be doing here so again um just like the the download file we're going to need to bring in pretty much all of these packages except the os we don't need the os um i guess i don't need it over here either so i'm not sure why i have it here but uh let me go ahead and remove the os don't need that all right so in this file we're gonna have three arguments so let's go ahead and set those three arguments right quick right the first one which is um argument number one would be root directory pass of files to upload right so i'm going to call this root directory system and this would be index one right so remember index zero is always the file that you're executing by default index one is whatever other argument we pass in so this is going to be our our let's say we have files and some kind of local directory that's that's what this is going to be but we need to specify the full directory pass so i'm using windows right now and so it'll be like your c drive you know wherever your file to that folder folder subfolder subfolder then until you get to the folder that you want to be if it's a network drive you specify the server uh name and then the file and so on right so all right so that's number one number two this would be our sharepoint uh folder name right this is on where we want to save it to in sharepoint may include subfolders to upload to all right so let's call this sharepoint folder name um this would be our index two right it's our second argument so index two the third argument that we're going to pass in would be um file name pattern so this is that we only want to upload specific file names with a specific name right so let's see have a folder that folder has files that's called sales calls you know sales and calls but you only want to upload these sales reports right the file with the word cells in it that's a pattern that we're looking for and that's where you can specify pattern it would only upload those file names with that pattern so that's kind of what this is going to be for only upload files with this pattern all right and this would be file name pattern system uh this will be index three all right so now that we have that out of the way what we're gonna do next is we're gonna need to create a few uh functions that we're gonna create here to do different things right i'm just separating the code uh and i'll build it here's the thing i could build a real quick example to show you that it works i want to build something that's more usable where you can literally take what i have and just populate your own arguments and you're good to go like you're it's that easy good to go right that's the whole that's what i'm trying to do here right so what i'm gonna do here is i'm gonna my first function that i'm gonna create i'm gonna call it git file context so this um it would be read files and um return the context of file right so that oh that's what this is doing right so file pass this is it's not text file this could be any file excel pdf doesn't matter but ultimately based on whatever if it's a true file we want to be able to read ex you know get the data from that file um and then ultimately that context is what's going to get uploaded to sharepoint so that's what this function is going to do right isolating it that way so we're going to do with open in this case will be file pass uh read binary as file then what i'm going to do here is i'm going to return oh it will be file dot read right so ultimately we're reading a file and that's what we're calling this read method here to be able to read everything like in that file then we're going to return that back it takes one file at a time that's what i call get file not files so just we're doing one file at a time that's what this does all right so the next uh method that we're gonna create is gonna be uh we're gonna call this get list of files right files they want to specify the folder so what this is trying to do what this is is doing better yet is we're going to specify a folder and we want to be able to return back a list of files from that folder but we're going to build logic in here where we only want to return back files right so you could have a folder that has subfolders inside we don't want to return the subfolders because those are not files they're folders we so we need a logic to identify is this a true file and if it is a true file return that back but if it's a folder don't return it back we don't want you know folder subfolders we don't want files so that's kind of what we're doing here right so i'm going to create a file list list that's going to be empty all right uh next i'm going to do a folder item list which this would be our oh you know what i am using os nevermind i need to bring that back all right so i am using the os um list directory and that's where i'm going to specify my folder so if you're not familiar with this the old list directory function pretty much you provided a um a folder pass it'll give you all of the objects right the items in that folder but again it gives you everything back it gives you subfolders and files right so that's where we need to build logic to only return back files only so once we do that we're going to do a for loop and we're going to call this for item in folder item list uh i'm going to call this i want to build my item full pass the reason why i want to build this i want i want the actual full the root pass and the file name is um i'm going to need that in order to be able to read the context so we'll see how we use that in a minute but that's the reason why i'm building this i'm going to use per pass again peer pass strongly recommend use all your projects because it it's um um it works on all operating system it kind of helps build a path based on operating system right so it makes it easier when you're bouncing around from from windows to linux and stuff or mac so i'm going to specify folder and then i'm going to specify item okay because the item is going to give me back um so what i'm going to do here though i'm actually going to put some logic before that so before that i'm going to do if os pass is file so this returns true or false oh you know what never mind i do need that not bad i need to put that before yep and the reason why because i'm gonna need this item full pass because again in order for in order for um this function is file to check to you know tell you true that it is a file or false that is not a file you need to provide the full route folder name pass right so that's kind of where this comes in and then it's able to check it is a file and if it is a file then that's where i'm gonna get my file list and i'm gonna append and now in this case i'm appending a list and the reason why it's a list because i want to add two uh values to it one of them will be item which item is the file name the second one is going to be the item uh full pass so i have the the the file name then the full file name past what i have here and then that's where when it's all said and done i'm going to return the file list so again just kind of iterate we got two functions now i get file context then we have a get list of files this will return back a list of true files in the form of a list so i mean that that makes it easier because if not you gotta then you gotta add logic to well if it's a folder i wanna skip it this again i'm just isolating our logic to different functions pretty much what i'm doing here right so now now that that's done the next thing that i'm going to create is going to be um upload file right so ultimately this is our function to upload a file like one file right that's kind of that's the purpose of this so the argument gonna be file name then it will be uh folder and then the context right the data of the file the data the file that we're trying to upload in this case we're going to end up calling sharepoint dot upload file and then we're going to pass in file name folder again folder the sharepoint folder name then of course the uh context that's it that's all we need to pass in here to upload file and again this function ultimately is calling the sharepoint you know um the next function that we're gonna create we're gonna call this upload files right with an s and what we're going to end up taking here is going to be a folder argument right so the reason why it's a photo argument because it's upload files more than one so we want to specify the folder which is going to be the root directory path so which folder we're looking for then by doing that that's what we're going to end up calling this function over here so what we're going to end up doing here we're going to call this file list equals the get list of files function that we set over here which again returns back a list so this is going to just return back a list of files only now directory subdirectory files only right so kind of has it already cleaned once it does that now we're gonna do an iteration over a four file in file list and then we're gonna need to get the file context of each file and that's what we're going to do called the file context and it will be get file context then we're going to specify in here file index one so again why index one remember down below we appended oops we appended a list index 0 the file name index 1 is the full root pass with file name so that's where we're going to in order for us to read the context we need to specify not just the file name but the full root directory with file name and then that's where it would be able to read it right over here be able to read that file and return back the data so that's why it's index one once we do that next we're going to end up calling upload file right which is this function here and then we're going to specify um what we need so one thing i was thinking right now and honestly we probably probably should have done it should do it this way makes sense there's no there's really no need to even call this or have this function this could just get moved over here because this is not doing anything else but that and that and that only right so because of that let's go ahead and comment that out for now and again file name in this case so kind of going back to this list file is a list object indexes 1 is a full root pass with file name index 0 the actual file name so i'm going to pass in here would be file index 0 which is the file name right then for folder this is going to be the sharepoint folder name and then of course we have our file context here so this would be file context and that's it this as it iterates over the list of files it will go up it will start uploading each file to sharepoint and this is not needed i don't need to do that um so that's it guys that's oh yeah we know what we need one more so we have upload files so i'm going to create one more let me create this here in the middle this is going to be called upload files um by pattern and this would be keyword and then it would be the folder so what we're doing here is again kind of going back to the file name pattern if maybe we have a folder that has 500 files but out of the 500 files maybe you have a file that are by customer right like in the name it has a customer name you know google apple facebook right just making up but you have client names in the file well you can specify a pattern that says hey find only the ones with the name google in it um and then we're gonna upload those files you know over to um to sharepoint right this is an example but um that's kind of where we're going to create a function for that as well then we'll add some logic on how to like use it and so on so in this case it will be the same concept as this um hold on why this oh yeah i'm in my back because of this so it'll be the same thing we need to get the list we're going to iterate over um and then what we're going to do here is going to be i'm going to add some logic that says if regular expression search um in this case it would be by keyword and then it would be the file name remember name which is index 0 if it has that name if that is a match then it will be the same where we do this so probably what we could do so we don't like reuse too much code under upload files we could go ahead and include a keyword equals none right so what we'll do here would be um we'll add some logic the logic that we could add would be if key a word is none um [Music] let's see how do we what do we want to do here so we could do if keyword is none execute this at the same time though i don't want to repeat this um i could do or or right because if it's not none that's when the or executes a regular expression search keyword uh comma file index zero now this has to be true so ultimately saying if keyword is none or if the keyword matches the file name which is true then execute this which means i do not need this i can just get rid of that it's not even needed so now we kind of instead of having another function for that we just kind of eliminated that and added it to here but again we don't need to provide a keyword only if we do provide a keyword then ultimately um so we do have technically a keyword and i may need to what i mean because it's not truly none right so what i mean to do this is change it to an actual um let me see uh let me remove it you know what i may either just remove it which is fine let me leave it as none because you may want to play around with it a little bit differently is if keyword is none i want to do another or or call this keyword um is not is not none right so if it's not none then we do want to run this oh my bad is none we do want to run this as well or if if it's none of these which means it doesn't have a keyword that's when you have a match needs to be true then it runs this all right so that should be fine so now that we have three pretty much three functions right to get context from the file to read it get a list of all the files then all time we're gonna upload and then you know that's kind of what we're doing here we're uploading um now let's go ahead and add this to our so this would be a name oops i had it wrong so it'll be name equals main and then from here we would do um upload files and then we're going to end up passing in a few keywords which in the first one would be root directory the next one would be um file name pattern and really that's it that's all we would do here yep i think that's it so what i'm going to do for this example is let me go ahead and i'm going to create a folder and let's call this storage and i'm going to add some files to that folder so let me go ahead and add some files to that folder so we kind of see this in action so let me go ahead and find some files real quick um all right cool so i found some let's see now let me go back to my project all right so storage and what i'm going to do as well is i'm going to go ahead and create a folder just call it test now i'm going to copy some of these in here right which is fine but you can tell we got one folder we have four files right that's kind of what we're dealing with here so let me go ahead and copy this because again i need to provide the full root pass of the folder where i want to upload files you know from then i'll automatically share point so now let's open let me go to sharepoint now this is not sharepoint um okay so i'm going to upload it to this folder here's right so you have document data 2022 in cells hit refresh there's nothing in there so now let's go ahead and run this and i'm going to upload files to there so i'm going to run python upload file right that's argument 1 0 index 0 pretty much for my argument the first argument if we look back is the root directory path this is where the files are going to come from to upload the sharepoint double quotes then i'm going to hit space right next is going to be where the sharepoint what is my folder called so now let's go over look at sharepoint so see how it says documents data blah blah blah so we're not specifying documents so keep that in mind do not do not include documents what you are going to include though is going to be in this case if i go to documents this is my main folder here my starting folder so be data 2022 and cells so i'm going to specify these for my folder um on where i want it to get imported uh uploaded to so it'll be data 2022 sales right so now i'm going to hit space and then my third argument is file name pattern so we'll do a test on that but for right now i'm going to have this as none okay right none now let's hit enter okay no error so far it looks good yes all right let's see let's make sure that it worked so now if i go back to sharepoint hit refresh boom we have all of our files here so it worked it worked fine right it did exactly it uploaded to the sales folder pretty much it did exactly what we wanted it to do um so let's try it again but see how we have in this case we have contacts so that i'm going to run it where i only want the context full files to get uploaded only right so let's do that real quick to make sure that logic works so that's deleted so now what i'm going to do is same thing i got to specify oops i just realized you can't really see my bad guys so again your file upload files.py this is gonna be the directory where your files are at then this is gonna be where you're gonna upload it to in sharepoint like the folder pass and sharepoint then i put none initially because i didn't have no patterns that i was looking for well now i do have a pattern that i'm looking for which is going to be uh contact i believe right uh damn i deleted the files you know i have them right here there goes contacts so you know i don't need to put the s because it's a wild card so contact should work so now if i hit enter let's make sure that logic works it should only upload those two files and boom there it goes it uploaded those two files exactly as expected so so again it wasn't too bad um we really just had to add one function to our office 365 api file and then of course i you know the upload file kind of shows i put we we created our code to kind of be isolated to do different things to get the context in one function to you know generate our our list of files and another function then of course the third one was the the actual uploading um so hopefully this helps out guys again this is a part two of the series there will be more parts coming you know i'm gonna be showing on uploading um i'm not uploading downloading sharepoint lists will be another one and prob also uploading to sharepoint let's say you want to create a new sharepoint list maybe you have an excel file for example or something some kind of data set and you want to create a new sharepoint yeah you want to take that file and ultimately convert it to a sharepoint list i'm going to go through a lot of these examples we're going to be continuing to upload update this source code so go take a look at it on the my github account and as i keep making new videos it'll get updated with the new um the new functionality that we're working on for that specific video so again guys hope this helps go ahead and give me a like email follow man appreciate it and be on the lookout for part three soon peace
Info
Channel: I am Lu
Views: 12,406
Rating: undefined out of 5
Keywords: sharepoint, python, office365
Id: pVYuUycBVNo
Channel Id: undefined
Length: 34min 11sec (2051 seconds)
Published: Tue Jul 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.