Node.js File Upload to Google Drive: A Beginner's Tutorial #googledriveapi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
are you looking to create an application which will upload a file to google drive well you're in the right place this is linda again and welcome back to my channel today we're going to be using a service account to upload a file to my google drive account using node.js and the google apis node.js client library the google drive api has a endpoint called files create the files create endpoint allows us to create new files up on google drive and upload them it's basically a two-step process the google drive api also supports service accounts service accounts are like dummy users they allow us to create applications that won't require user interaction in order to authorize them that's because we can pre-authorize the service account granting it the access that we need just remember not all google apis support service accounts but the google drive api does the first thing we will need to do in order to run this code is we need to create a service account to do that we go to google cloud console create a new project once you've created a new project you then go to credentials and create a service account you will need to create a new key file and i recommend that you create a json key file and not the p12 file then you need to go to libraries and enable the google drive api and that's it you should have a service account key file that will now work with the code that we're going to create google has been kind enough to release client libraries that we can use in order to access their apis from the languages that we're using for this video we're going to use node.js so we are going to be using the nodejs client library we will need to install the google apis package so i'm going to do an npn install on google apis which will install the package into my project our code needs several constants in order to run i'm going to create those first at the top of my file the first constant i need is the path to the key file which we downloaded from google cloud console which identifies our service account the second constant that i need is one called scope the scope constant will identify which scopes my application needs in order to run scopes are used in oauth to define the amount of access an authenticated user is granted when accessing the api due to the fact that we will be using a service account i tend to just grant it full access as i'm the one controlling the service account anyways so in my opinion it doesn't really matter that much so i'm just going to add scope of drive which is the full access drive scope in order to use the service account in our application we need to initialize a google auth object we do this by passing it the key file path and the scope this object will then be used in the next step the way google built up this client library is that all calls to an api must go through a service in our case we're going to be accessing the google drive api so we need to create a drive service when we create a drive service we pass it the version of google drive which we would like to add and we pass it the authorization which we created earlier this authorization added to the drive service will then ensure that all calls that we make to google drive through this service will automatically be authorized the code itself will handle adding the authorization header to our calls we don't have to deal with this anymore creating a file up on google drive is actually done in two parts the first part is the metadata for the file we need to create a metadata object for our file the metadata for the file would be things like its name its description mime type and in our case i'm actually adding parents as well because i would like the file to be uploaded to a specific directory on my google drive account this directory i shared with the service account earlier so i know that the service account has access to write to it so by creating a new file for this case i am going to be creating my file and uploading it to the proper directory the second step to creating a file is to create the actual file stream for the file data itself in this case we're just loading the mime type which is the mime type of the file and creating a file stream of the file on my hard drive now we have everything we need to create our file so i'm going to call drive service files create and i'm going to pass it the metadata as a resource the file stream and then i'm also going to pass it fields all fields does is limit the response and in this case all i really want to get back is the id i don't need all of the file information back so i'm just going to pass fields and then id which will ensure that i only get the file id back in my response now we just need to handle the response the response coming back from the api will actually have a status code on it and i find it easier just to ensure that the status code returns a 200 and if it does then i know my file was created properly and i output the file id if it doesn't come back as a 200 then i know there was an error and i can just log the error to the console now when i run my application you will see that it actually outputs the file id and if we check up on google drive sure enough it uploads a file there as well as you can see uploading binary files to google drive is actually quite easy you just have to remember that it takes place in two parts you send the metadata then you send the file stream they both need to be included if you don't send the metadata your file will be called untitled or unnamed and if you don't send the file stream your file won't actually have any data it'll just have a name this method will work with binary type files if you want to upload a non-binary file for example something like a csv file that you would like to have converted to a sheet then you need to set the mime type for it to cause it to be converted i will be putting up a video on how to do that shortly if it's already up then a card should be appearing here now well that's all for now i hope you enjoyed this video and please remember to give it a like and share and subscribe if you've enjoyed my content it really helps me out and it helps me to know which videos you liked and which ones you'd like to see more of and as always i hope you have a really great day
Info
Channel: DAIMTO Developer Tips
Views: 23,598
Rating: undefined out of 5
Keywords: Daimto, Google Developers, Google Developer Expert, Linda Lawton, Google apis, Google development tutorials, google drive, google drive api, google drive api tutorial, How to use google drive api with node.js, node.js and google drive api, upload files with node.js, upload files to google drive with node.js Google service accounts, how to use service accounts with node.js
Id: Z2MCxblgPoc
Channel Id: undefined
Length: 10min 21sec (621 seconds)
Published: Wed Apr 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.