20

I want to download data from google drive link shared by someone using google colab. I am a new user of colab and I don't know how to do that. the links are

x_train: https://drive.google.com/open?id=1cUaIEd9-MLJHFGjLz5QziNvfBtYygplX

y_train: https://drive.google.com/open?id=1hv24Ufiio9rBeSqgnNoM3dr5sVGwOmBy

x_test: https://drive.google.com/open?id=1AH9lKHT5P2oQLz8SGMRPWs_M9wIM2ZRH

y_test: https://drive.google.com/open?id=1i4_azocSDuU3TcDf3OSHO1vF0D5-xMU6

Thanks in advance

Fu678
  • 324
  • 1
  • 2
  • 7
  • I'm not sure if this can be done or not (maybe others can weigh in), but what I usually do is to add the files to my own Google Drive or GitHub and get them from there. – rchurt Jul 06 '20 at 20:36

3 Answers3

48

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')
korakot
  • 37,818
  • 16
  • 123
  • 144
  • I tried to download a folder with your library but it is asking for a verification code. What code does that mean? Is it possible to specify it in code so I don't have to type it at console prompt? – RKCZ Mar 18 '21 at 09:30
  • 3
    @korakot I'm getting an error with kora at your "import download" line in drive.py: ----> 3 drive.download_folder('1HvIeNhqtFVllXFWzH5NawDdnIfgGDwCK') /usr/local/lib/python3.7/dist-packages/kora/drive.py in download_folder(folder_id) 60 urlretrieve(url, path) 61 # recursive download ---> 62 import download ModuleNotFoundError: No module named 'download' – sh37211 Aug 10 '21 at 03:30
7

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 <^_^>

fdsig
  • 268
  • 2
  • 5
1

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/...