The Ultimate Guide: Uploading Files to Google Drive with NodeJS using Google Drive APIs #nodejs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign how to upload a file to Google Drive by making use of node.js with Google Drive API so let's get started so here is an article been written by me so in this article I have covered each and everything like creating a Google Cloud console account and creating a project over here so here I have just defined all the steps what you need to do you can just refer this article or you can just continue with this video tutorial okay so in this Twitter in this article I have covered each and everything like creating a service account getting the API okay so you so basically we need a Json file for getting authorized to use our Google Drive and upload the file to our Google Drive so basically we need to create a project through which we can access Google Drive apis okay so this is the step it will provide a Json file for you and the provided Json file we can use it in our node.js code and access our Google travel and upload the file to that drive okay so let's get started here you can just refer this thing okay so let's go to Google console so you can just uh search for Google console and just go to this Google Cloud console the page you can see over here and this page will get open and here you just need to create a new project like this so here you just need to give a project name so I will just create a new project okay so it is it is creating a project for me so it may take some time so you can see a project has been created so just select this project okay so project has been created so now uh we need a API key to access our Google Drive so for that we need to create a service account okay so to create a service account first of all we need to enable Google Drive API so let's do that so just go to API server okay so you can just go from here also or you can go from the main page also and here just go to libraries and here search for Drive okay so this is Google Drive API so you need to enable link so enable okay so after enabling you can see I am in Google Drive API so now here I need to create a credential uh that will be a Json file to access our Google API so just go to this credentials or you can just go over here and create it so I will go over here okay and now here create credentials service account so we need to create a service account okay and I can just give any name to the service account so I will just name it as test1 and done okay so the service account has been created in our this project uh under Google Drive API okay so now just go to this service account email you can see okay and now here you can see there is a tab by name Keys just go to that page and here create new key okay so the key it will be of type Json it has been recommended so just created okay so you can see a new Json file has been downloaded over here okay so this is our API key okay so that's it in our Google Cloud so now we can just navigate to our node.js project and integrate Google Cloud API uh in our project so let's get started okay so now over here I will just create a new uh folder and I will just initialize it to a node.js project so just name it as node Drive something so I'll just open this folder okay and open my terminal to create this normal folder into node.js project so to create a node.js project you just need to initialize it by entering npm init and just enter the details about your project you can just author and everything okay so node project has been created okay so I'll just open this in Visual Studio okay so you can see this is node.js Project opened in Visual Studio so I will just create a file and I will just name it as app.js JavaScript okay so now basically uh we are going to upload a text file to our Google Drive so for this we are going to make use of a library that is FX library to read the file and to communicate with our Google apis we are going to make use of a model called as Google apis so we just need to install them so here npm install FX library and Google apis so these are the two libraries that is required to be installed in our node.js project so uh as you remember that we have created a API key and the file of that is of type Json has been created over here so let me copy this file into our folder okay and I'll just rename this file and I will just name it as API dot Json okay so this is our API key okay so through this API Json file that is the API key we can access our Google Drive account so now let's start our coding part so here first of all we need to import FX Library okay and then we also need a Google uh API so require Google API and from Google API we only need Google okay and then we need to access this Json file so I will just store that Json file into a variable so I'll just keep that variable as API Keys is equal to require and here I will just give the path to our Json file so now in this variable all the details about this Json has been stored in this variable so first of all we need to write two functions one is authorization that is authenticating author author authenticating to our Google drive and second function is uploading a file to our Google Drive okay so here I will just create first function that is authenticating and I will just name this function as authorized okay so this function is responsible for authenticating or giving connection to our Google Drive okay so here const JWT client is equal to new Google dot auth dot JWT okay so here Google comes from this library that is Google API okay so here we need to pass three uh some parameters from this apis for authenticating so first parameter will be API key dot client email address so here you can see client email so this is the client email the second parameter will be null so there is no need of to send the second parameter the third parameter will be a private key to access our Google Drive and uh third fourth parameter is a scope okay so the scope is basically what feature we are going to make use of from this API so basically here we are going to make use of Google Drive so let's create a scope over here that is the URL of which feature that we are going to make use from our Google apis so this is basically a URL okay so the URL is so you can just get this code from my article as I shown you so here I have full code you can just refer this thing okay and now await JWT client dot authorized and finally return the authorized client okay so authorize function has been created next thing is we need to create a function that will upload the file to our Google Drive so here let me create a sync function upload file or client so here we are going to pass auth client so this function will handle a promises that is resolved or rejected so written new promise result rejected okay so now create a variable that will hold our Drive authentication and I will just name it as Drive Google dot drive and version that we are going to make use of our Google Drive so the version is V3 that we are going to use and authentication authentication and authentication will be our fourth client that the user has signed in okay so now here we need to pass what data we need to send to our Google trust that is a text file or any other file that we are going to upload in our Google trade so here I will just create a variable that will hold a metadata of our document So Meta data name of the file okay so here will be a name of the film and metadata to which drive this document should be submitted so which folder of your Google Drive the documents will be submitted so here you just need to give the folder name okay so let me show you okay so here is my Google Drive so inside this Google Drive I have created a empty folder a folder I will just delete this thing and inside this test folder I'm going to I'm getting an error to download this okay okay I don't know why okay so it's okay so here in this drive I am going to upload our any document so I want to just get this folder name that is the ID folder ID so just copy this ID and paste it over here okay so this will be our finite metadata okay so now finally we need to upload the file to our drive so Drive dot files dot create okay so here we need to pass three parameters one is resource that is metadata median that is the file that we are going to upload and then the field the field will be our ID okay so this field is basically used so that whenever a file has been created we get the ID or the file ID that is been uploaded to the Google Drive so here uh in the media body property we need to upload a file so to upload a file we are going to make use of a library that is FX library that we have already imported and by making use of upload FX Library we are going to read the file that we want to upload to our drive so create read stream and the path to the file that we want to upload so what I will do is I will just create a dummy file over here okay so I'll just name it as test dot text okay so this is the txt file that I want to upload to our Google so I will just give the path of this felt so the part is text Dot txt okay and as we know that this is the txt file and we need to Define mime type so mime type is text plane okay so once this uh has been completed we must handle the the updates by making use of callback function so here I will just Define a callback function if there is any error error will be shown and if the file has been successfully uploaded to our drive then we the record will be shown in this file parameter if error found then just return resulted with the error okay and if not then resolved with file name okay okay so now finally we need to call this function when the authorization has been successfully authorized so here I will just call this function after authorization has been completed so here I can just do is auth authorization dot then upload upload file dot catch error if there is any error then we will just catch it over here so now to run this file all you need to do is in node app.js Okay so by doing this our file will get uploaded okay so here it is showing an unauthorized some print credentials okay so let me check okay so here I'm just getting this file not found error that is because this user that is this email ID has not been assigned to access any Drive of our Google account so you just need to give permission to access this folder so you just need to save this folder with our services that we have created so let me show you okay so this is the service account that we have created so we need to give permission for this service account to use our Google drive this folder so you just need to save this folder with the service account that has been created and just keep it editor option so that the user will be able to edit or add any files to this drive okay so send this okay so this may take some time okay so after completing just run the code again so let me run okay app.js let's wait so you can see I did not get any error now so let's check our Google Drive so you can see file has been uploaded successfully so you can see this is the file that will get uploaded to Google Drive using node.js Code okay so it is just showing Untitled let me show Untitled okay so I have missed something I think so okay yeah so here I did not pass any name to this file so here I will just pass this name same thing okay so save the file and run it again let's check so uploading so I can just give a message over here that is uh file has been uploaded or anything so just do it local log upload started like this and after completing the uploading part you can just ding okay okay something like this you can just give a message so let me try okay started to upload it okay so let me check it so you can see this file has been uploaded to this type okay so that's all for this video tutorial thanks for watching please do subscribe protocols Point thank you
Info
Channel: Proto Coders Point
Views: 9,370
Rating: undefined out of 5
Keywords: node js, node js tutorial, google drive, google drive api, google drive api nodejs, google drive api node, google drive api nodejs example, google drive service accunt, how to get google drive api key, google drive api json, how to example google drive api, how tocreate new project in google cloud console, upload file to drive using nodejs
Id: bkaQTLCBBeo
Channel Id: undefined
Length: 15min 45sec (945 seconds)
Published: Wed Aug 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.