'Mount multiple drives in google colab

I use this function to mount my google drive

from google.colab import drive
drive.mount('/content/drive', force_remount=True)

and then copy files from it like this

!tar -C "/home/" -xvf '/content/drive/My Drive/files.tar'

I want to copy files from 2 drives, but when i try to run first script it just remount my 1st drive

How can i mount 1st drive, copy files, then mount another drive and copy files from 2nd drive?



Solution 1:[1]

The colab drive module doesn't really support what you describe.

It might be simplest to share the files/folders you want to read from the second account's Drive to the first account's Drive (e.g. drive.google.com) and then read everything from the same mount.

Solution 2:[2]

Just in case anyone really needs to mount more than one drive, here's a workaround for mounting 2 drives.

First, mount the first drive using

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

Then, use the following script to mount the second drive.

!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
!mkdir -p /drive2
!google-drive-ocamlfuse /drive2

Now, you will be able to access files from the first drive from /drive1/My Drive/ and those of the second drive from /drive2/ (the second method doesn't create the My Drive folder automatically). Cheers!

Fun Fact: The second method was actually a commonly used method to mount Google drive in Colab environment before Google came out with google.colab.drive

Solution 3:[3]

If you're getting an exception with Suyog Jadhav's method:

MessageError: Error: credential propagation was unsuccessful

Follow the steps 1 to 3 described by Alireza Mazochi https://stackoverflow.com/a/69881106/10214361


Follow these steps:

1- Run the below code:

!sudo add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!sudo apt-get update -qq 2>&1 > /dev/null
!sudo apt -y install -qq google-drive-ocamlfuse 2>&1 > /dev/null
!google-drive-ocamlfuse

2- Give permissions to GFUSE

From the previous step, you get an error like this. Click on the link that locates in the previous error message and authenticate your account.

Failure("Error opening URL:https://accounts.google.com/o/oauth2/auth?client_id=... ")

Script output signaling link to click

3- Run the below code:

!sudo apt-get install -qq w3m # to act as web browser 
!xdg-settings set default-web-browser w3m.desktop # to set default browser
%cd /content
!mkdir drive
%cd drive
!mkdir MyDrive
%cd ..
%cd ..
!google-drive-ocamlfuse /content/drive/MyDrive

After this step, you will have a folder with your second drive.

Result

Solution 4:[4]

There is Rclone which is a command-line program to manage files on cloud storage. It is a feature-rich alternative to cloud vendors' web storage interfaces. Over 40 cloud storage products support rclone including S3 object stores, business & consumer file storage services, as well as standard transfer protocols.

In this url you will find how to set it up in Colab. you can even link one drive and other cloud storage products too.
https://towardsdatascience.com/why-you-should-try-rclone-with-google-drive-and-colab-753f3ec04ba1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ami F
Solution 2 Suyog Jadhav
Solution 3
Solution 4 isaac Adam