21

I was trying to download file from my google drive to colaboratory.

file_id = '1uBtlaggVyWshwcyP6kEI-y_W3P8D26sz'

import io
from googleapiclient.http import MediaIoBaseDownload

request = drive_service.files().get_media(fileId=file_id)
downloaded = io.BytesIO()
downloader = MediaIoBaseDownload(downloaded, request)
done = False
while done is False:
  # _ is a placeholder for a progress object that we ignore.
  # (Our file is small, so we skip reporting progress.)
  _, done = downloader.next_chunk()

downloaded.seek(0)
print('Downloaded file contents are: {}'.format(downloaded.read()))

doing so am getting this error:

NameError: name 'drive_service' is not defined

How to remove this error?

A Santosh
  • 609
  • 1
  • 7
  • 9

7 Answers7

37

No installing/importing any library. Just put your file id at the end.

!gdown --id yourFileIdHere

Note: at the time of writing gdown library is preinstalled on colab.

hoper
  • 503
  • 5
  • 11
15

the easiest method to download a file from google drive to colabo notebook is via the colabo api:

from google.colab import drive
drive.mount('/content/gdrive')

!cp '/content/gdrive/My Drive/<file_path_on_google_drive>' <filename_in_colabo>

Remarks:

  1. By the drive.mount(), you can access any file on your google drive.
  2. 'My Drive' is equivalent 'Google Drive' on your local file system.
  3. The file_path is surrounded with single quotes as the standard directory below the mount point ('My Drive') has a space, and you might also have spaces in your path elsewhere anyway.
  4. Very useful to locate your file and get the file path is the file browser (activated by click on left arrow). It lets you click through your mounted folder structure and copy the file path, see image below.

enter image description here

Agile Bean
  • 3,541
  • 1
  • 23
  • 41
7

Here's an easy way to get by. You may either use wget command or requests module in Python to get the job done.

# find the share link of the file/folder on Google Drive
file_share_link = "https://drive.google.com/open?id=0B_URf9ZWjAW7SC11Xzc4R2d0N2c"

# extract the ID of the file
file_id = file_share_link[file_share_link.find("=") + 1:]

# append the id to this REST command
file_download_link = "https://docs.google.com/uc?export=download&id=" + file_id 

The string in file_download_link can be pasted in the browser address bar to get the download dialog box directly.

If you use the wget command:

!wget -O ebook.pdf --no-check-certificate "$file_download_link"
Dex
  • 509
  • 5
  • 4
3

Step 1

!pip install -U -q PyDrive

Step 2

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)

Step3

file_id = '17Cp4ZxCYGzWNypZo1WPiIz20x06xgPAt' # URL id. 
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile('shaurya.txt')

Step4

!ls #to verify content

OR

import os
print(os.listdir())
shaurya uppal
  • 2,335
  • 23
  • 28
2

You need to define a drive API service client to interact with the Google drive API, for instance:

from googleapiclient.discovery import build
drive_service = build('drive', 'v3')

(see the notebook External data: Drive, Sheets, and Cloud Storage/Drive REST API)

user2314737
  • 21,279
  • 16
  • 81
  • 95
2

I recommend you use Pydrive to download your file from google drive. I download 500MB dataset for 5s. 1. install Pydrive

!pip install PyDrive

2. OAouth

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

3. code for download file from google drive

fileId = drive.CreateFile({'id': 'DRIVE_FILE_ID'}) #DRIVE_FILE_ID is file id example: 1iytA1n2z4go3uVCwE_vIKouTKyIDjEq
print fileId['title']  # UMNIST.zip
fileId.GetContentFile('UMNIST.zip')  # Save Drive file as a local file

Cheer Mo Jihad

Mohamed Jihad
  • 458
  • 3
  • 5
0

You can also use my implementations on google.colab and PyDrive at https://github.com/ruelj2/Google_drive which makes it a lot easier.

!pip install - U - q PyDrive  
import os  
os.chdir('/content/')  
!git clone https://github.com/ruelj2/Google_drive.git  

from Google_drive.handle import Google_drive  
Gd = Google_drive()  
Gd.load_file(local_dir, file_ID)
Jean-Christophe
  • 101
  • 1
  • 4