Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to connect google colab to my google drive for good?

I found this great question here: https://stackoverflow.com/questions/48376580/google-colab-how-to-read-data-from-my-google-drive which helped me to connect the colab to my drive

Here it is as well:

from google.colab import drive 
drive.mount('/content/gdrive')

My question: Is there anyway to do this process of the google authentication only once? Colab disconnects from time to time if not in use and than I need to restart the authentication process.

Thanks

like image 222
Giladbi Avatar asked Nov 01 '25 19:11

Giladbi


2 Answers

Authentication is done per machine; exchanging keys to access drive. Since you always get a new machine on re-connect, you need to re-authenticate.

However, another option is to use an API key for your google drive access. This can be done via theGoogle API Console for the Drive Platform. Essentially you would have one API Token you can over and over again; possibly leading you to store it inside the notebook... where the bad part starts.

If you opt-in on using a token to "manually" mount the drive folder, as soon someone gets a hand on this token (i.e. sharing your notebook, man in the middle, forgetting to delete the key), your drive folder is compromised. That is the reason why my formal answer to this question is: No, you can't.

But since colab provides the whole machine with a unix environment where you can execute arbitrary bash commands, you are in control and leave you with additional resources for further investigation:

  • https://stackoverflow.com/a/50888878/2763239
  • https://medium.com/@uditsaini/access-google-drive-and-mount-google-drive-to-colab-notebook-google-ccbca1691d31
  • https://github.com/googlecolab/colabtools/issues/121#issuecomment-423326300
like image 83
Dino Avatar answered Nov 04 '25 08:11

Dino


A recently released feature makes this much simpler. The details are described in this answer: https://stackoverflow.com/a/60103029/8841057

The short version is that for notebooks in Drive that aren't shared, there's now a GUI option to mount Drive files automatically for a given notebook.

like image 38
Bob Smith Avatar answered Nov 04 '25 10:11

Bob Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!