我从这里下载了启动代码:http://goo.gl/Tcxkod并按照说明从此处安装 sample.py
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 读取?
我点击了开发文档,除了 repr 和源代码本身,没有其他线索:https://developers.google.com/resources/api-libraries/documentation/drive/v2/python/latest/drive_v2.files.html#get
最佳答案
使用 PyDrive
我测试了PyDrive我认为这非常直截了当。这是我所做的:
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
这段代码有两个“问题”:
- 每次使用它时,它都会在浏览器中询问您是否同意。阅读this page让他记住你的回答。
- 如您所见,您需要记住文件的唯一标识符 (id)。您可以将它们放入数据库或本地 json 文件中。您还可以使用 PyDrive 来 list all files matching a query ,从而为您提供其 ID。
没有 PyDrive
如果您对使用 PyDrive 不感兴趣,我强烈建议您阅读它的代码。如果必须这样做,我会在 pydrive.files 中放置 3 个断点(或打印):
- 在
_FilesInsert
行self.auth.service.files().insert(**param).execute()
- 在
_FilesUpdate
行self.auth.service.files().update(**param).execute()
- 在
_DownloadFromUrl
行self.auth.service._http.request(url)
和构造函数中查看他如何获取 url(它在self.metadata
)
您在sample.py
中下载的代码已经有了服务
,所以您只需要知道param
字典中的内容了解正在发生的事情。
但是,您应该使用 PyDrive ;)
关于python - 如何通过 python API 将 json/pickle 文件转储和读取到 Google Drive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20971442/