Downloading data from a shared google drive link in google colab

Viewed 12678
3 Answers

You can use gdown if the file is shared publicly.

!gdown 1cUaIEd9-MLJHFGjLz5QziNvfBtYygplX

If it's shared to you only, you need to use pydrive

# Install the PyDrive wrapper & import libraries.
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.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

file_id = '1cUaIEd9-MLJHFGjLz5QziNvfBtYygplX'
downloaded = drive.CreateFile({'id':file_id})
downloaded.FetchMetadata(fetch_all=True)
downloaded.GetContentFile(downloaded.metadata['title'])

If they share a folder, it's too long so I made it short in my library.

!pip install kora
from kora import drive
drive.download_folder('1HvIeNhqtFVllXFWzH5NawDdnIfgGDwCK')

A great question and something that I have been working with for some time. The most seamless/ workflow friendly is using gdown.

Presently Colab has a slightly older version install which does not allow full functionality and is installed on pyton2.7 rather than Colab system python. therefore following the terminal/underlining os via `!` method, `!pip installing`:
!pip uninstall gdown -y && pip install gdown
!gdown -V

Then you can use gdown via another ! (method 1) or import gdown (method 2) if you want to use it in code:

Method 1 for whole shared folders/directories:

!gdown --folder https://drive.google.com/drive/folders/sdsldkfj...somefileid.. -O /some_parent_directory/some_child_directory

for files:

!gdown https://drive.google.com/drive/folders/sdsldkfj...somefileid.. -O /some_parent_directory/some_child_directory

Method 2 Using down via importing works as follows:

import gdown
url = 'https://drive.google.com/uc?id=0B9P1L--7Wd2vNm9zMTJWOGxobkU'
output = '20150428_collected_images.tgz'
gdown.download(url, output, quiet=False)

Pasting your google drive file/directory url in both cases.

Hope this helps <^_^>

If you have a link to someone's publicly shared file on Google Drive, you can do this in 4 mouse clicks:

  1. Create a shortcut to this file in your Google Drive (right-click on file -> "Add shortcut to Disk")
  2. Mount your Google drive in Colab notebook (click buttons: "Files" -> "Mount Drive")
  3. Now you can access this file via your shortcut for !unzip/np.load/...
Related