如何通过 python API 将 json/pickle 文件转储和读取到 Google Drive? [英] How to dump and read json / pickle files into Google Drive through the python API?

查看:49
本文介绍了如何通过 python API 将 json/pickle 文件转储和读取到 Google Drive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里下载了启动代码:http://goo.gl/Tcxkod并按照说明安装了<代码>sample.py 从这里 https://developers.google.com/api-client-library/python/start/installation:

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.

但是如何将 pickle/json 文件转储到驱动器上,然后检索它以从 python 中读取?

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

我点击了开发文档,除了 repr 和源代码本身之外没有任何线索:https://developers.google.com/resources/api-libraries/documentation/drive/v2/python/latest/drive_v2.files.html#get

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

推荐答案

With PyDrive

我测试了 PyDrive,我认为它非常简单.这是我所做的:

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:

  • 每次使用时,它都会在浏览器中询问您是否同意.阅读此页面,让他记住您的答案.
  • 如您所见,您需要记住文件的唯一标识符 (id).您可以将它们放在数据库或本地 json 文件中.您还可以使用 PyDrive 来列出与查询匹配的所有文件,从而为您提供其 ID.立>
  • 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.

如果你对使用 PyDrive 不感兴趣,我强烈建议你阅读它的代码.如果必须这样做,我会在 pydrive.files 中放置 3 个断点(或打印):

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:

  • _FilesInsert 行上的 self.auth.service.files().insert(**param).execute()
  • _FilesUpdate 行上的 self.auth.service.files().update(**param).execute()
  • _DownloadFromUrlself.auth.service._http.request(url) 和构造函数中查看他如何获取 url(它在 self.metadata)
  • 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)

您已经拥有 service 和您在 sample.py 中下载的代码,因此您只需要知道 param 字典中的内容了解正在发生的事情.

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.

但是,您应该使用 PyDrive ;)

However, you should use PyDrive ;)

这篇关于如何通过 python API 将 json/pickle 文件转储和读取到 Google Drive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆