Search code examples
google-cloud-platformgoogle-drive-apigoogle-api-python-client

Mount Google Drive from AI Platform Notebook


Is there a way to mount Google My Drive from Google's AI Platform Notebook like Google Colab?

Thanks, Jay


Solution

  • Yes, there is. You can use Python package gdrivefs to mount Google Drive to AI Platform Notebook. You can find exact information for setup from PyPi project page here: https://pypi.org/project/gdrivefs/.

    To install in AI Platform Notebook you need to either install it at terminal or use shell commands in Notebook. With the following commands you should be able to do it:

    Install dependencies and gdrivefs package:

    !sudo apt-get install -y build-essential python-dev
    !pip install gdrivefs
    

    Authenticate to your Google Drive with the link provided from first command and pass the auth token to the second command:

    !gdfstool auth_get_url
    !gdfstool auth_write "xxx….”
    

    Mount and list files. You should see Google Drive content:

    !gdfs -o allow_other default /mnt/gdrivefs
    !ls /mnt/gdrivefs
    

    Now you can use the mounted files as local files:

    import pandas as pd
    df = pd.read_csv('/mnt/gdrivefs/mydata.csv')
    print(df)
    

    If I recall correctly the performance with this method is not very good. This works at least for temporary access, small data and copying data to more suited location.

    To do the same in CoLab you need only the following code. It is much simpler and more performant but unfortunately google.colab does not work in AI Platform Notebook:

    from google.colab import drive
    drive.mount('/mnt/drive')