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

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


With PyDrive

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

from pydrive.auth import GoogleAuthfrom pydrive.drive import GoogleDrivegauth = 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 filesame_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 ;)