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) # Create and upload a text file uploaded = drive.CreateFile({'title': 'Sample upload.txt'}) uploaded.SetContentString('Sample upload file content') uploaded.Upload() print('Uploaded file with ID {}'.format(uploaded.get('id'))) # Load a file by ID and print its contents. downloaded = drive.CreateFile({'id': uploaded.get('id')}) print('Downloaded content "{}"'.format(downloaded.GetContentString())) # Drive REST API from google.colab import auth auth.authenticate_user() from googleapiclient.discovery import build drive_service = build('drive', 'v3') # Creating a new Drive file with data from Python # First, create a local file to upload. with open('/tmp/to_upload.txt', 'w') as f: f.write('my sample file') print('/tmp/to_upload.txt contains:') !cat /tmp/to_upload.txt from googleapiclient.http import MediaFileUpload file_metadata = { 'name': 'Sample file', 'mimeType': 'text/plain' } media = MediaFileUpload('/tmp/to_upload.txt', mimetype='text/plain', resumable=True) created = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() print('File ID: {}'.format(created.get('id'))) # Downloading data from a Drive file into Python file_id = created.get('id') 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()))