Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dump and read json / pickle files into Google Drive through the python API?

I've download the start code from here: http://goo.gl/Tcxkod and followed the instructions and installed the sample.py from here https://developers.google.com/api-client-library/python/start/installation:

alvas@ubi:~/git/UniversalCorpus/drive-cmd-line-sample$ python sample.py
Success! Now add code here.

But how do I dump pickle/json files onto the drive and then retrieve it to read from python?

I clicked on the dev documentation and there's no clue other than the repr and source code itself: https://developers.google.com/resources/api-libraries/documentation/drive/v2/python/latest/drive_v2.files.html#get

like image 569
alvas Avatar asked Jan 07 '14 12:01

alvas


People also ask

How do I view JSON files in Google Drive?

Go to Google API Console. Go to the Credentials page. Click the Download JSON button to download the client secret JSON file and securely store it in a local folder. This JSON file can then be used by Google Drive components and metadata wizard to access Google Drive via the OAuth method Installed Application (JSON) .


1 Answers

With PyDrive

I tested PyDrive and I think it's pretty straigtforward. Here's what I did:

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

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

# Upload a json file (will be identical for a pickle file)
json_file = drive.CreateFile()
json_file.SetContentFile('wopwop.json')
json_file.Upload() # Files.insert()
print json_file['title'], json_file['id']

# Download the same json file
same_json_file = drive.CreateFile({'id': json_file['id']})
same_json_file.GetContentFile('test.json')  # Save Drive file as a local file

There's two "problems" with this code:

  • It asks you in a browser if you agree everytime you use it. Read this page to make him remember your answer.
  • As you can see, you need to remember the unique identifier (id) of your file. You could put them in a database or in a local json file. You could also use PyDrive to list all files matching a query, thus giving you its id.

Without PyDrive

If you are not interested in using PyDrive, I strongly suggest you to read its code. If I had to do it, I would place 3 breakpoints (or print) in pydrive.files:

  • in _FilesInsert on the line self.auth.service.files().insert(**param).execute()
  • in _FilesUpdate on the line self.auth.service.files().update(**param).execute()
  • in _DownloadFromUrl on the line self.auth.service._http.request(url) and in the constructor to see how he obtain the url (it's in self.metadata)

You already have service with the code you downloaded in sample.py, so you only need to know what is in the param dictionary to understand what's going on.

However, you should use PyDrive ;)

like image 116
Nil Avatar answered Dec 11 '22 16:12

Nil