Last active
July 12, 2018 11:36
-
-
Save kleysonr/27fa2a50410d57a79b0fe6ef7eef906c to your computer and use it in GitHub Desktop.
Download and sync files from Google Drive into Google Colab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For the first time, install PyDrive | |
# !pip install PyDrive | |
import os | |
from pydrive.auth import GoogleAuth | |
from pydrive.drive import GoogleDrive | |
from google.colab import auth | |
from oauth2client.client import GoogleCredentials | |
# 1. Authenticate and create the PyDrive client. | |
auth.authenticate_user() | |
gauth = GoogleAuth() | |
gauth.credentials = GoogleCredentials.get_application_default() | |
drive = GoogleDrive(gauth) | |
Debug = 'Yes' #@param ["No", "Yes"] | |
# 2. Auto-iterate using the query syntax | |
# https://developers.google.com/drive/v2/web/search-parameters | |
def DownloadFiles(driverFolder, localFolder): | |
# choose a local (colab) directory to store the data. | |
local_download_path = os.path.expanduser('%s' % localFolder) | |
try: | |
os.makedirs(local_download_path) | |
except: pass | |
file_list = drive.ListFile({'q': "'%s' in parents" % driverFolder}).GetList() | |
for f in file_list: | |
# 3. Create & download by id. | |
if Debug == 'Yes': | |
print('title: %s, id: %s, type: %s' % (f['title'], f['id'], f['mimeType'])) | |
if f['mimeType'] != 'application/vnd.google-apps.folder': | |
fname = os.path.join(local_download_path, f['title']) | |
if Debug == 'Yes': | |
print(' downloading to {}'.format(fname)) | |
f_ = drive.CreateFile({'id': f['id']}) | |
f_.GetContentFile(fname) | |
else: | |
DownloadFiles(f['id'], '%s/%s' % (local_download_path, f['title'])) | |
Download_files = 'No' #@param ["No", "Yes"] | |
Google_drive_folder_ID = '<folder_id_here>' #@param {type:"string"} | |
Local_base_dir = '~/data' #@param {type:"string"} | |
if Download_files == 'Yes': | |
DownloadFiles(Google_drive_folder_ID, Local_base_dir) | |
print('Finished.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment