Build a Node.js Express Google Drive API With OAuth2 to Upload,Delete & Download Files in Browser
Jan 9, 2025
Get the full source code of application here:
https://github.com/gauti123456/googledriveclone
Show More Show Less View Video Transcript
0:00
uh hello guys welcome to this video so in this video we will try to build out a
0:06
mini Google Drive clone inside nodejs Express with full authentication and we
0:12
will provide the operation we will let the user to upload a file download a file delete a file and
0:19
display the files from their respective Google drive this is actually the application I'm running this on Local
0:26
Host port number 5,000 so it's a simple nodejs express application where we
0:32
actually have this login with Google button so first of all you need to authenticate with your Google account so
0:39
as I click the button I will be redirected to my Google account I need to select the Google account and it will
0:46
show you this warning that Google hasn't verified this app because you are building it on Local Host that's why you
0:51
need to click on continue and then you need to verify the access just grant the permission here click on continue so
0:59
once you grant to permissions you will see all the files will be shown right here the latest files from your Google
1:05
Drive I'm showing the top latest 20 files so you can see the name of the
1:11
file alongside with the extension you will see that so we also have the form
1:18
where we allow the user to upload a file to Google Drive so if I select any file
1:23
image video any file we can select so as I select this file you will see the the
1:28
file will be selected and we have this button if I click upload file so you
1:34
will see it will take some time and after that the file will get uploaded
1:40
and you will get this notification the file will get updated right here and we provide these buttons as well if you
1:46
want to download this file or delete this file and as I click this file this is clickable I can click this file and
1:53
you will see the file has been successfully uploaded you can see the Google Drive Link this is the actual ID
1:59
which is given by Google drive after they upload the file so this is actually my file here I can download this file by
2:06
clicking the download button so as I click the download button you will see the file will get downloaded and you can
2:12
see I can open the image file or I can click the delete button as
2:17
well if you want to delete this file as I click the delete button you will see
2:23
the file will get removed and if I visit the same link once again you can see
2:30
the file is will be deleted so the file no longer is there
2:41
so so this is my Google Drive here you can see that file has been
2:46
removed so you can see no file is present that file is removed and you can
2:52
see we have showing the latest 20 files from my Google Drive so if you want to
2:57
download any of these files you can click click the download button as well so it is directly interacting with the
3:05
Google Drive API in order to perform these operations to download files delete files display files or upload
3:11
files so we are actually uh interacting with the Google Drive API so you can select here any
3:19
image file click on upload file and you will see as soon as the file will get
3:26
uploaded you will see you will get this notification and here you can see the file is
3:31
uploaded so you can download the file you can click the link and it will actually show you this is the file is
3:38
uploaded so we will be building this application from scratch in nodejs
3:43
Express let me show you briefly which Technologies we will be using in this tutorial so I will show you all the
3:51
modules one by one so first of all we are using Express module which is actually used to build out your web
3:58
server inside nodejs it's a very popular uh app package in nodejs then we will be
4:06
using Google apis this is actually the STK where we interact with the
4:14
directly we uh do the authentication part before we use the Google Drive API
4:20
so this is actually a API client if you want to use any Google API in noj Express you install this module so it's
4:27
very popular module 2 million weekly downloads and we will also be using embedded JavaScript templates which is ejs which
4:35
is used to embed Dynamic JavaScript inside your HTML we'll also be using this and now to upload files we will
4:42
also be using molter which is a nodejs package if you want to upload files
4:47
inside your application we'll also be using this module as well so all the source code will be given in the
4:52
description and uh lastly if you we will
4:58
also be storing the information in the sessions so that's why we using this package specifically designed for
5:04
Express applications which is Express session so it's a session middleware where if you want to store some
5:10
information across sessions you can use this package so now in the next section we will start building this application
5:17
from scratch so I will be seeing you in the next section uh now to get started
5:22
guys I'm there inside my project directory I will come out of it and I will make make
5:29
a new directory I will call this as Google Drive clone I will CD into this
5:36
and then I will open this inside vs code Visual Studio code so I'm using this
5:41
editor for building this application so now to get started we need to initialize
5:47
npm in a-y so this will actually create your package.json file with the default
5:55
options so in the left hand side you will see package. Json file will get
6:00
created now we need to install the modules I showed you in the last section so Express will be the web server molter
6:08
will be used for file upload ejs is a template engine and Google API with the
6:13
module to interact with to do the authentication part and lastly we need to also install a module EnV which is
6:22
used to store useful information inside your environment
6:28
variables and also Express session which is used to uh store
6:34
variables inside session these six packages I use express malter ejs Google
6:40
apis. EnV Express session so install this by executing this
6:52
command so now you can see all the modules is installed you node modules folder has
6:58
been generated created and inside dependencies we using the latest versions of all these modules
7:06
so just close this file and just create your index.js file which will actually
7:11
hold all your code where you'll be writing the express code for starting a
7:16
basic Express server so we'll be requiring the Express package so
7:23
you'll simply say require and inside Express I will create a very basic
7:29
Express app and uh I will start this
7:34
application on port number 5,000 and in the call back
7:43
here we will say that that the app is listening on Port
7:49
5,000 and then now we also need to set the view engine which is ejs that we
7:56
installed so we can directly set this using the set method and we can in the second argument we can pass
8:03
EGS and here when we go to the homepage we need to load a template so all the
8:09
templates by default in ejs is stored in the views folder just create a views
8:15
folder and inside this create your basic index EGS you can call this anything I'm
8:23
just calling this right here this will be a basic page which will contain the
8:28
login button where we will do the authentication so for building this page uh we will use
8:36
some basic bootstrap so for including the bootstrap we need to include the CDN
8:42
so what I will do I will simply paste the CM just after the
8:50
title this is your bootstrap cdan and then we will actually have a very basic
8:57
button inside this container boot strap class you have this heading which will
9:02
say Google Drive mini clone and after that we will
9:08
have a very basic button which will say login with Google so we will have this anchor tag
9:16
and for now this anchor tag will simply say login with Google that's so it's a
9:23
very basic page that we designed now we just need to show this page when the user goes to the
9:29
so we can just route write this route and now to load this template we use response. render and just provide the
9:36
file name not the extension that's all so if you now go to the just start this
9:44
application so we are using a module node mod which will start your
9:49
application as you make changes
9:59
so if you open this application now so we can just open it Local Host
10:06
5,000 so you will see that template is loaded and it is actually showing this template so now we
10:14
need to actually do the authentication part here so for doing the authentication we need
10:21
to import that Google apis module like
10:27
this and from from this we are importing this Google method and now for doing the
10:34
authentication we do need to require the client ID and client secret
10:40
and just create a EnV file we storing these two information right here so you'll create two variables
10:47
which is first of all Google client ID and Google client secret so both this
10:54
information you need to create a Google Cloud console account and uh you need to
10:59
first of all enable the apis I have already in my dashboard so
11:05
just click the enable API services and here you need to select your API Google
11:11
Drive API and just enable this API before you make the project so I've
11:16
already enabled this so after this you need to go to credentials and then you
11:22
need to create the O client ID and the application type is web application and
11:29
here in the authorized JavaScript Origins you need to paste your url of your we actual website that you are
11:36
building so mine is Local Host 5,000 so I will simply paste
11:42
this and in the authorized redirect URL we need to I will customize this so This
11:50
is actually your call back URL so whenever the user is authenticated the user is redirected to this URL so this
11:59
you can name this anything I'm just naming it as o or2 call back so I will
12:05
Define this route in my Express app so for now I can click the create button to
12:10
actually get my client ID so you will see this is actual client ID this will
12:16
be different for you so just inside this EnV file just paste
12:22
your client ID and also you will get the client secret as well so copy that also
12:29
and paste it so after doing this now we just need
12:35
to load this two variables from the environment variables for loading this we need to import the module which is
12:43
EnV so just require this
12:50
module and then we just need to say EnV config it will actually load all the
12:56
variables from your EnV file
13:02
so now I will create the variables here Global variables in the in this file so
13:09
first I will call client ID so we can easily get using process. EnV and
13:16
whatever the variable that you call so Google client ID the second variable will be for the client secret so CLI
13:25
secret again we can get that using process. EnV and and Google uncore
13:30
client secret and the third URL will be for the
13:38
redirect URL so whatever redirect URL you you
13:45
actually given right here so in my dashboard so you need to give the same
13:52
URL that you gave at that location so o or2 call back
13:59
so now we have declared all these three variables the client ID client secret and redirect
14:04
URI now we need to actually make
14:10
a uploads directory just right here in the project directory here all your
14:16
files will get stored if you want to upload a file to Google Drive so first of all the
14:22
file will be uploaded using malter just create this uploads directory and now we need to
14:30
include some middle Wares so the first middleware we need to include right here using app.use is the body pass
14:37
middleware so we can express. URL encoded extended to false and the second
14:44
line is express. Json so these two lines are responsible because we are working
14:50
with forms because the user will be allowed to upload files so that's why we need to transfer
14:57
information from the form to express apps that's why this body pass two lines of middleware is required so just
15:03
include this and uh that's all I think that we need to do
15:10
right here and now we need to modify this Home
15:15
Route instead of loading this template we will first of all do the
15:20
authentication so for doing the authentication we need to generate the URL for that so we'll create a new
15:27
variable Au URL and right here whatever you call your o or2 client
15:34
I think we haven't created this to client so we need to now create the O client using
15:43
that information so you'll create a new variable so I will call this as o 2
15:50
client and uh I will instantiate a new instance of this Google do.
15:59
o 2 and here you need to provide all this three information client ID client
16:05
secret and the redirect Ur So now we will get our o client right
16:14
here you can see now we can simply use this client and this contains a method
16:21
in order to generate the URL so there is a method called as generate Au URL and it actually takes an
16:29
object which contains these two properties that you need to pass right
16:36
here first is the access type so this needs to be set as offline the second
16:42
option is the scope scope refers to which information that you are accessing from the API so here we need to
16:48
explicitly mention the scope so we will be requesting
16:54
the OD Drive scope so this enables you to
17:01
upload files delete files download files all the operations you can perform using Google Drive so this is a scope so we
17:07
are providing the scope right here so once you now we actually generate this
17:13
URL now we need to pass this URL to the template so we will render the template
17:21
but now you will pass this object and this object will contain this Au URL
17:31
so you can see we are entering the template but this time as the second argument we are passing this object which contains this all URL so we are
17:38
passing this
17:46
information so this needs to be same so I will say Au
17:52
URL so now in the index. ejs route we need to dynamically reference this this variable
17:59
that we are passing so right here inside your anchor tag in the hrf attribute we will
18:06
dynamically insert this variable so for doing this we'll be using the EGS syntax
18:12
like this and followed by the variable name that we passed so spelling mistake
18:17
must not be there you need to exactly type the variable that you passed like
18:23
this and I will give a bootstrap class let me say BTN BTN
18:29
primary so this will give it a blue color so also give it a class to The
18:35
Heading as well which is margin bottom 3 so if you check out your application now
18:41
margin bottom 4 you will see a button right here login with Google
18:47
so if I click this button you will be redirected to your actual Google account
18:54
that you want to select so once you select the account you need to Grant the permission right here click on continue
19:00
and now it will show you this error that you haven't defined your redirect URL so
19:05
we need to Define this redirect URL this is the same redirect URL you are uh the
19:11
user is redirected to this once they are authenticated we get this authorization code if you see in the URL we now need
19:19
to exchange it with the access token so now we need to Define this uh
19:26
route redirect URL so what I will do uh I will Define this
19:33
call back URL so app.get o or2 call back and right here
19:41
you will Define this route and here we can get our authorization code from request.
19:48
cy. code so now we can exchange this for Access token so we can say o or2 client
19:55
and it contains a method get token so here we need to pass your
20:01
authorization code as the first argument and the second argument will be a call back function here we'll be getting our
20:07
access token so now we need to save this token
20:14
in the session so for using the session in
20:19
Express we need to require a package for this so we can simply require at the very top so we need to pass it as a
20:27
middleware so this is specifically for nodejs and express application so I will
20:33
call this as session and we need to require this package which is Express
20:38
Express session so now we need to pass this as a middleware so for passing it
20:44
as a middleware we can use it right here at the very top so
20:51
app.use and we can simply say Express or we
20:57
can uh we can directly pass it as a
21:03
middleware yeah so we can say session and here it takes some properties so we
21:10
need to set a secret key this can be anything if you are developing it for production just make sure your secret is
21:16
uh very safe so for this example I'm just calling it as secret key and then
21:23
it has the second option is resave which is a Boolean parameter
21:29
so this is false save uninitialized let me set it to true so these options you need to
21:36
pass and then we have the cookie it will be an object and secure is false so this
21:44
if you are using on production https then you need make it true by default it is false so now we have set all this
21:51
information in the express session now we can easily save this access token in the session so what we can do
21:59
we can simply write this line which is request dot session and now the session
22:06
is variable uh session is available in the request object so we can make a
22:11
property called as token and we can store this token that we are getting access token in the
22:18
session so then we can redirect the user using the redirect function and we
22:25
can redirect them to the/ dashboard route for this we will be creating a new
22:32
page in the views folder so I will call this as dashboard. EGS so this will be the dashboard page once the user is
22:39
authenticated they will be redirected to this
22:48
page so I will simply give it a heading this is the dashboard so once the user
22:53
is authenticated now we just need to make this route app.get SL
22:59
dashboard and what we need to do we just need to load this template response. render
23:08
dashboard so what will happen now if you refresh now you will see it will be redirected to the dashboard route so
23:15
once you are authenticated you will see this dashboard route so once again if you click login with Google so now
23:22
automatically you will be redirected to the dashboard page so in this way we are
23:28
storing the access token the session and then we are redirecting the user to the
23:34
dashboard page so now we just need to make sure that we need to make this
23:40
route as protected route this dashboard because if if I just open this
23:47
application in a new incognito window I can still go to the dashboard
23:53
page without having authenticated so if I open Local Host 5,000 and again if I
23:59
in the URL just type SL dashboard I can directly go to the dashboard page
24:04
because in this way I am not authenticated so we need to protect this route for protecting this route we need
24:10
to write a function so this function will be responsible for checking if the
24:16
user is authenticated or not so I will call this function as is authenticated
24:22
so the pro this function will take the request
24:27
response and next middleware so it simply check that if the token is present in the session or
24:34
not so if request. session token returns true in that case the user is
24:39
authenticated then we need to set the credentials so set credentials and we
24:44
can set the token request. session. token and then we can say just redirect
24:52
the user to the route that they are requesting but if the if it if it
24:58
returns false if the token is not present in that case we just need to redirect the user to the homepage so you
25:07
can say redirect them to the homepage so now we need to pass this
25:13
middleware as as a middleware right here so in between these we can just make
25:19
this route as protected now any user going to this route will
25:25
get this function will get executed we will check so now if you go to that
25:31
again if you visit SL dashboard
25:36
now directly if you type this in the URL you will now see you you can't go to the
25:43
dashboard page you will be redirected to the homepage because you are not authenticated so in this way but here if
25:50
I go to SL dashboard I do need to do the authentication now and again you can see
25:59
now I am authenticated so in this way you can make your route protected so now what
26:07
what I need to do right here we need to actually make the dashboard page for
26:15
making the dashboard page we do need to retrieve the files from the Google Drive so right in this route SL dashboard you
26:22
first of all need to get the number of files from the Google Drive for doing
26:27
this we need to call the Google Drive API so Google Das drive and here we need
26:34
to specify which version we are using so we are using the version three and Au
26:40
here we need to pass over authentication client as the second argument and now we
26:45
have authenticated so right here we need
26:52
to use the API so this drive it contains the files list function so here we need
27:00
to specify how many files we want to return in the page size so we need to return the top latest 20 files and the
27:08
second argument is the fields so which Fields you want to return specifically will specify here so I want the ID of
27:15
the file name and the MIM type so you can see we have explicitly
27:22
providing which fields that we want which is ID of the file name MIM type which is extension
27:29
so after you provide this this will actually return a call back function
27:34
right here so put a comma and this will return
27:40
your all your files it will contain in the response so this is a call back function
27:49
and we can console log the response it will contain a data property
27:56
which will contain the files so if I just uh do this again go to the
28:02
dashboard page if you check the console you will see all the files will be returned top
28:08
20 files will be returned to me this is actually an array of objects each object
28:14
contain three properties which is a MIM type ID and the name so all the properties that we mentioned right here
28:20
are returned to us the top 20 files from my Google Drive now what we need to do
28:25
we need to show this files in the browser for showing it we will render reender the template by using the render
28:33
function dashboard and this time we need to pass this data to the template which is
28:39
files and file property which will say response. data files so I will delete
28:46
this line so now we are passing a property called as files this will be an array
28:52
here now we need to go to dashboard so again we will be using
28:59
bootstrap so just include the CDN after the
29:05
title so I've included the bootstrap CDM so right
29:10
here we will display all the files in the table so we'll be using some bootstrap
29:19
classes we'll align the heading in the center position which will say Google
29:24
drive file viewer I will simply call this
29:31
and right here we will use some bootstrap class
29:37
bootstrap is not necessary for this tutorial I'm just using it for styling purposes so I will not explain
29:44
bootstrap so I will just have a heading which will say the Google files in
29:52
Google Drive and then we will have a simple table where we'll be showing this data so
29:59
so so you giving some bootstrap table
30:06
striped so you'll have the table head tag so inside this we will have two
30:13
headings which will be the file name and the second column will be for
30:19
actions which will contain the download button delete button so just after this table head tag
30:27
we will have the table body tag and inside this we will Loop through we will
30:32
use the for each Loop so files for each and for each file we will
30:42
be so just need to surround these brackets because this is a syntax of EGS
30:48
just make sure that you put these brackets and in between these we will
30:54
display the HTML so here here in the TD tag First Property will be
31:03
the the actual name and we will making a hyperlink as well so in the TD tag we
31:10
will have anchor tag and here will be putting the name of the file which is uh file do
31:19
name so in this way we can put variables inside HS so right in here we need to
31:25
make it hyperlink so what we will do
31:30
we will make it as a hyperlink like
31:36
this so this is a syntax of the actual link of the Google Drive drive.google.com file /d followed by
31:44
here we are putting the actual ID of the file which is file. ID dynamically so if you refresh your
31:51
application now so you will basically see all the f
31:58
will get displayed right here you can see that these are the latest files in my
32:05
Google Drive so all these files have been
32:10
hyperlink so you can
32:16
see so you can open this actual file here this is a TST file you will see
32:23
that so now we are displaying the files right here from my Google
32:31
Drive so in the actions here in we have the second column for the actions which
32:38
will actually contain the buttons for downloading the file deleting the file
32:43
so we will have these two buttons in the second column so we again have anchor tag this button
32:51
will be for download the second button will be for delete
33:04
so for the download we will make the route here so I will what I will do I will copy
33:14
this so here we need to make a new route SL download and we are basically
33:19
embedding the file ID and similarly for deleting we will
33:26
have a simple form which will be responsible for deleting the file so in this way if I can just format
33:35
the document so you can see we have a one button for download the second button
33:41
will actually contain a form we'll make a simple post request so here SL delete
33:48
slash the actual ID of the file so if you just refresh your
33:55
route so you will see the two buttons will be displayed the download button and the delete button so if I click the
34:02
download button you will see it will say cannot get we need to make this get route so you can see dynamically the ID
34:08
of the file is passed in the argument in the URL so we need to make these two routes
34:15
for downloading and deleting so what I will do I will simply move my to my
34:20
express code so for downloading it this will be
34:26
a dynamic route so app.get SL download and here we
34:33
need to specify the file ID that we are getting as Dynamic parameter I'm just
34:40
calling it as file ID so again this needs to be protected
34:46
route so I will just use the middleware is authenticated the user must be authenticated to download the
34:52
file so here now to actually fetch the file ID
34:58
we will use request. params which is a very basic way in Express to actually
35:04
get the parameters from the URL so now after getting the file ID
35:11
it's very easy to download the file in Google Drive we will use a function in
35:16
this SDK Google R drive again here we need to again
35:21
specify the version and the O client so writing the same line
35:28
so here we have the function drive files get and here we need to specify the file
35:36
ID which is the file ID that we got and the
35:42
fields so what fields we are specifying the
35:49
name so we are passing two things right file ID and the fields so we only need
35:55
to get the name of the file so this returns again a call back
36:03
function so it will return the name of the file
36:08
metadata I will just call this as metadata
36:15
response so it will return your file name inside this metadata response
36:22
variable we will have the data and the name of the file returned to us
36:28
first of all we need to find out which file the user is uh just downloading so we are actually
36:35
making the first request to find out the name so this will be file name will be
36:40
returned to us now using this file name we will fetch the content of the file we
36:46
will make a second rest call to the API F uh Google Drive API this is a get
36:53
method which is available so here we need to specify y first of all the file
37:00
ID so we pass the same file ID that we got and the second parameter here we
37:06
need to pass is alt parameter so here we need to download the file that's why we are providing
37:12
this as media and the second thing that we need
37:18
to pass put a comma the second object is the response type so here we need to
37:24
stream it because we are downloading it that's why we are provided stream so again put a comma this returns
37:31
a call back function so error or file
37:37
response it's a call back function so now to download this file as an
37:44
attachment we can simply do this inside Express by first of all setting the
37:50
headers these two headers we need to set which is content disposition
37:59
so here we are simply setting the headers which is content disposition to
38:05
be attachment and file name is the actual file name that we retrieved and then we can simply use the
38:12
pipe method file response. dat. pipe and
38:19
we will pipe the response so this will simply download the file as an attachment so what you will see if I
38:25
reload the page click the download button uh can
38:31
download any file let me click download so what will happen you will see the file will get downloaded this is a txt
38:39
file so the file is opened you will see very easily so in this way you can even
38:45
download a image file as well so this is the image file so this also get
38:51
downloaded you can see it's a JPG file so in this way the download button is
38:56
working now I will will come to the delete button as well delete if you press it you will see cannot post again
39:02
it is passing the file ID but this time this is a post request so now we need to
39:08
make a post request for the delete operation so here this will be very easy
39:16
because here we are passing the file ID that we want to delete
39:30
so now for the delete delete button we will make a post request so just after
39:36
the download here so we so this is our code right
39:42
here so this time this is the post request so you'll say app. poost and here slash delete followed by the
39:50
parameter that we receiving which is the file ID colon file ID this will also be
39:56
a authenticated so we are passing this middleware request
40:01
response so here we need first of all fetch our file ID Again by using
40:07
request. params do like this so then again we'll be
40:15
writing the same line we need to specify so any operation that you
40:20
perform you need to write this line so specify which version that you're using and pass your o client after that this
40:28
there is a straight method in Google Drive API which is the delete function so Drive files. delete this will delete
40:37
the file which will have this file ID so we are passing the file ID right here so this returns a call back
40:46
function so this will delete the file and here you can simply show a
40:51
notification that your file is deleted file deleted and then we can
40:57
refresh the browser as well so you can simply redirect again to the dashboard page this will actually delete that file
41:05
and refresh it in the table as well so that's all so if you again now go to the
41:21
page so let's suppose I want to delete this txt file so if if I click
41:29
delete so you will see that file will get deleted and we will also get a message
41:35
that in the console if you
41:42
check okay so the we are redirected back to the that's
41:50
why that message so the delete function also working so now we'll come to the
41:56
upload part part so if you want to upload a file to Google Drive this operation is little bit
42:04
complicated but I will try to teach you in a very easy way so first of all for upload purpose
42:11
we do need to upload the file to the express server for doing this we need to
42:17
slightly do the configuration for molter so right at that very top we need
42:23
to import the molter package so just require
42:30
it then we also need the file system package which is a built-in package in
42:36
nodejs so we are requiring both these packages and then we need [Music]
42:42
to write the molter part if you use
42:53
molter we just need to tell which we
42:58
where the molter
43:04
will upload the files so for this we just need to add this line this line
43:09
will exactly tell molter where to upload the file so we are simply passing it in
43:14
a property in the object destination so here we are simply telling we have
43:20
explicitly made a uploads directory so up molter will first of all
43:26
upload this file and then it will upload to Google Drive so now for making this
43:34
uh we need to update this dashboard route we need to include a form where we
43:39
allow the user to upload a file in right just before the table we can include a
43:45
simple form uh
43:52
write yeah after this heading we can have a simple form
43:59
just inside this so you can have a form the action
44:07
will redirect the user to/ upload and we make a simple method will be post and
44:14
encoding type multiart form
44:20
data so inside this we will have a simple input type file and we giving a name
44:28
parameter as well file class will be form control it's a bootstrap class and it should be required then we'll be
44:35
having a simple button which will say upload
44:41
file and the button type here will be of submit class will be BTN BTN
44:53
success so if you refresh now you will see a simple form appearing so you can
44:59
see choose file button is there so I think it is slightly looking
45:05
uh so what I will do I will simply paste it I think we are just done some typo mistake in HTML so all the source code
45:13
is given in the description so if you paste you can now see the interface is looking quite good we have the choose
45:19
file button where as I choose the file let's
45:24
suppose I choose any file click on upload file you will see it is saying this message cannot post SL upload so
45:31
now we need to make this post request right here I can make it in
45:40
my so write a very simple post request app. poost SL
45:50
upload so for this upload operation first of all now we need to use that
45:57
middle we this will also be a is authenticated again we need to pass
46:03
because only authenticated users can upload the file to Google Drive and then
46:09
the second middle we which will be the malter one so we need to upload a single
46:14
file so we are actually passing two middlewares first for authentication then is it's a malter one so it contains
46:22
a function single to upload a single file and then it will be a call back
46:29
function so again we need to tell which version of Google Drive that we again we
46:36
to paste the same line pass your authentication
46:41
client then here now for uploading the file we first of all need to specify the
46:48
file metadata object which will actually contain the name of the file so whatever
46:55
name we can easily get using request. file. original name so malter provides
47:01
with this property so whatever is the original name of the file it will be given as the
47:10
name so now we need to specify the second object which is the actual media
47:17
so we will actually read this file by the file system module it contains a
47:22
function called as re create read stream and here will be passing the path of the
47:28
file which malter provides us with request. file. paath
47:34
so then we have a straight format method in Google Drive API to upload a file so
47:40
we simply say drive. files. create function we'll use this function and
47:45
simply here we need to pass an object which will contain these three properties which first of all we need to
47:51
specify the resource so it will be the file metadata object the second property is the media so the media will be
47:58
actually contain the actual file so it will read the binary content of the file
48:04
and the the third property is the fields so here it is just telling to the API
48:11
that uh after the file is uploaded we need to return the ID of the
48:18
file so that's all and then a call back function will be returned so it will be
48:25
error file so then you can simply show a notification to the user that the file
48:31
is uploaded and then again we can redirect the
48:36
user we can refresh the page so that's
48:43
all so now I can test this out and if I go
48:49
to the page
49:01
so it is giving me this error that cannot access is authenticated before
49:07
initialization uh just wait I just need to move this is authenticated function
49:14
at the top so just move this code right just before
49:22
this just paste it here because it must be declared before you call this so just
49:29
make the slide change and now the application will work so if you click the now we have this uh form right here
49:37
where we allow the user to upload the file so if I choose any file uh let me choose this image file JPG file and
49:45
click the button upload file so what will happen it will upload the file to
49:50
the Google Drive and then it will refresh the page so this is a newly uploaded file and if I click the link we
49:57
will see the file has been successfully uploaded we have got the link as well so
50:03
in this way you can see that we can either download this file as well by
50:08
clicking the download button and we can even delete this file
50:13
so if you don't need this file you can delete this so in this way using the
50:18
programmatically we are communicating with Google Drive API we performing both these four operations create read update
50:26
delete so in this way you can directly upload files download files
50:33
delete files so all the source code is given in the description of this video so thank
50:41
you very much for watching this video and do check out my website as well free mediat tools.com which contains
50:47
thousands tools regarding audio video and MH and I will be seeing you in the next
50:52
video
#File Sharing & Hosting
#Cloud Storage
