3

[ Newer Edit]: colab team reported that they corrected the issue on May 27 2020. I have checked - it works Okay for me now.
Link to issue: https://github.com/googlecolab/colabtools/issues/1205

==================================================================

[New Edit:] It became clear that problem below arises ONLY if mount the google drive to colab by via web interface button "Mount Drive" and does NOT appear if mount by command line way. So seems web way is bugged. See details in my own answer below. It is checked for "Chrome" browser.

==================================================================

[Original question:]

How to access "shared with me" from google colab ? (Interface seems changed now (2020) and previously described solutions does not seem to work).

More details:

The question has been asked several times, and the solutions described e.g. here : https://stackoverflow.com/a/53887376/625396 The problem that I do not see "Add to My Drive" , but see "Add shortcut to Drive". After doing it, we can see that via web-interface for google drive, that shortcut indeed appears.

BUT that shortcut canNOT be seen via colab utilities, like os.listdir() ! So shortcut seems to be invisible for colab, and not clear how to access it.

Below are the screenshot, showing that colab does not see the shortcut to "shared with me"-"cytotrace_datasets", but web-gui of google drive can see.

Here is screenshot what I see by colab (shortcut canNOT be seen): enter image description here

Here is screenshot what I see by web-gui of google drive (shortcut can be seen): enter image description here

Alexander Chervov
  • 616
  • 3
  • 7
  • 23

2 Answers2

4

Brief: do NOT mount google drive by web-interface button "Mount drive" (it is bugged), but do it in the "old" command line way, and you will not have problems.

Details:

After getting excellent answer above and playing with it, it seems I found some strange thing which results in simpler solution and probably indicates that there is currently a bug with mounting the google drive by the web interface button "Mount drive".

I mean do NOT mount the drive by interface: enter image description here

But do it in the old way: enter image description here

and that is all - you will get the access to files which added before with the help of "Add shortcut to Drive": enter image description here

Alexander Chervov
  • 616
  • 3
  • 7
  • 23
1

Suppose you want to read a shared csv file from drive. You have done "Add shortcut to Drive".

1) At Colab Notebook Connect to your drive.

# Import PyDrive and associated libraries.
# This only needs to be done once per notebook.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

2) Get the id of shared file you want to access. Open file -> go to linksharing [https://drive.google.com/open?id=1JKECh3GNry6xbAK6aBSzQtSntD4GTEl ] -> copy the the string after 'id='

3) back to colab

# A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
file_id = '1JKECh3GNry6xbAK6aBSzQtSntMD4GTEl'

downloaded = drive.CreateFile({'id': file_id}) #important
print(downloaded['title'])  # it should print the title of desired file
downloaded.GetContentFile('file.csv')  
#Finally, you can read the file as pandas dataframe. 
import pandas as pd
df= pd.read_csv('file.csv') 

Note : This is my first ever answer to a stack overflow question

  • Thank you very much for your excellent answer, it works and really very helpful ! Just a couple of remarks - which makes my understanding better: the command downloaded = drive.CreateFile({'id': file_id}) - loads file into memory, after that we can save it anywhere by downloaded.GetContentFile('destination_filepathname') - it might take a moment for system will do it, or need to press "refresh" to see the result. But it must appear. My file is not .csv, so it was not immeadiately clear for me what downloaded.GetContentFile('file.csv') was doing... – Alexander Chervov May 02 '20 at 13:57
  • That answer : https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url becomes helpful for me, after reading your answer – Alexander Chervov May 02 '20 at 14:08
  • The only problem which we potentially meet is that if need to do like this with many files, we need manually to get "link-sharing" . Is there any way round ? PS Meanwhile it seems I found that mounting google drive by web button seems to be bugged, and doing in command line way - does not cause the problem we fight with - see my own asnwer. Any way thank you very much ! – Alexander Chervov May 02 '20 at 14:18
  • Thank you for positive feedback. It turned out if use this code `from google.colab import drive # This will prompt for authorization. drive.mount('/content/drive',force_remount=True) import os os.chdir("/content/drive/My Drive")` We can use google drive like we use local file system in python. And it gives access to shared files as well. – Hafsa habib May 02 '20 at 15:09
  • What if you need to get complete data from a directory that is shared with us by someone.? – Susaj S N Aug 17 '20 at 05:09
  • You will add that shared folder in your drive. there is an option [add shortcut to drive]. then you can treat the shared folder as any of your folders in the drive. – Hafsa habib Aug 26 '20 at 03:48