-
-
Save ricardodeazambuja/a453024392b784363a6cb2942a0e77f1 to your computer and use it in GitHub Desktop.
Google drive upload, read, update with Google Colab
This file contains hidden or 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
# Install the PyDrive wrapper & import libraries. | |
!pip install -U -q PyDrive | |
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 & upload a new file. | |
f = drive.CreateFile({'title': 'Sample file.txt'}) | |
f.SetContentString('Sample upload file content') # or SetContentFile('im.png') | |
f.Upload() # see f.get('id') | |
# Read a file | |
fid = drive.ListFile({'q':"title='Sample file.txt'"}).GetList()[0]['id'] | |
f = drive.CreateFile({'id': fid}) | |
text = f.GetContentString() # or f.GetContentFile('im.png') to save a local file | |
# Update a file | |
fid = drive.ListFile({'q':"title='Sample file.txt'"}).GetList()[0]['id'] | |
f = drive.CreateFile({'id': fid}) | |
f.SetContentString('Some newer content') | |
f.Upload() |
This file contains hidden or 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
# combine upload new & update | |
res = drive.ListFile({'q':"title='Sample file.txt'"}).GetList() | |
if res: | |
fid = res[0]['id'] | |
f = drive.CreateFile({'id': fid}) | |
else: | |
f = drive.CreateFile({'title': 'Sample file.txt'}) | |
f.SetContentString('Sample upload file content') | |
f.Upload() |
This file contains hidden or 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
# from https://stackoverflow.com/a/49433955/6729010 | |
from google.colab import auth | |
from googleapiclient.http import MediaFileUpload | |
from googleapiclient.discovery import build | |
auth.authenticate_user() # click and fill | |
drive_service = build('drive', 'v3') | |
def save_file_to_drive(name, path): | |
file_metadata = { | |
'name': name, | |
'mimeType': 'application/octet-stream' | |
} | |
media = MediaFileUpload(path, | |
mimetype='application/octet-stream', | |
resumable=True) | |
created = drive_service.files().create(body=file_metadata, | |
media_body=media, | |
fields='id').execute() | |
print('File ID: {}'.format(created.get('id'))) | |
return created | |
# then use the function | |
save_file_to_drive(destination_name, path_to_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment