Search code examples
google-colaboratory

bug (resolved) with "Mount drive" web-button in colab. Accessing "shared with me" files from google colab (y2020, previous solutions seem to fail)


[ 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


Solution

  • 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