如何通过python API将json / pickle文件转储并读入Google Drive?

我从这里下载了开始代码:http://goo.gl/Tcxkod并按照说明安装了XP001中的sample.py:

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

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

我点击了dev文档,除了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;)

相关文章

使用OpenCV实现视频去抖 整体步骤: 设置输入输出视频 寻找帧...
前言 对中文标题使用余弦相似度算法和编辑距离相似度分析进行...
前言 之前尝试写过一个爬虫,那时对网页请求还不够熟练,用的...
前言 本文使用Python实现了PCA算法,并使用ORL人脸数据集进行...
前言 使用opencv对图像进行操作,要求:(1)定位银行票据的...
天气预报API 功能 从中国天气网抓取数据返回1-7天的天气数据...