google drive api很简单。
PyDrive3的Python项目详细描述
叉子
这只是pydrive repo的一个分支。这里的代码直接来自github。这是在pydrive与python3不兼容时创建的。但现在是了,所以这个包裹现在没用了。你应该使用谷歌的官方版本
PyDrive
pydrive是 google-api-python-client 这简化了许多常见的google drive api任务。
项目信息
PyDrive的功能
- 将OAuth2.0简化为具有灵活设置的几行。
- 将Google Drive API包装到 每个资源的类,使程序更面向对象。
- 帮助除api调用以外的常见操作,例如内容获取 以及分页控制。
如何安装
可以使用常规的pip命令安装pydrive。
$ pip install PyDrive
OAuth简化了
从google api控制台下载client_secrets.json,oauth2.0是 分成两行。您可以在一个设置中自定义OAuth2的行为 文件settings.yaml。
frompydrive.authimportGoogleAuthfrompydrive.driveimportGoogleDrivegauth=GoogleAuth()gauth.LocalWebserverAuth()drive=GoogleDrive(gauth)
文件管理变得简单
使用一种方法上载/更新文件。Pydrive最多能做到这一点 有效的方法。
file1=drive.CreateFile({'title':'Hello.txt'})file1.SetContentString('Hello')file1.Upload()# Files.insert()file1['title']='HelloWorld.txt'# Change title of the filefile1.Upload()# Files.patch()content=file1.GetContentString()# 'Hello'file1.SetContentString(content+' World!')# 'Hello World!'file1.Upload()# Files.update()file2=drive.CreateFile()file2.SetContentFile('hello.png')file2.Upload()print('Created file %s with mimeType %s'%(file2['title'],file2['mimeType']))# Created file hello.png with mimeType image/pngfile3=drive.CreateFile({'id':file2['id']})print('Downloading file %s from Google Drive'%file3['title'])# 'hello.png'file3.GetContentFile('world.png')# Save Drive file as a local file# or download Google Docs files in an export format provided.# downloading a docs document as an html file:docsfile.GetContentFile('test.html',mimetype='text/html')
文件列表分页变得简单
pydrive为您处理文件列表分页。
# Auto-iterate through all files that matches this queryfile_list=drive.ListFile({'q':"'root' in parents"}).GetList()forfile1infile_list:print('title: %s, id: %s'%(file1['title'],file1['id']))# Paginate file lists by specifying number of max resultsforfile_listindrive.ListFile({'maxResults':10}):print'Received %s files from Files.list()'%len(file_list)# <= 10forfile1infile_list:print('title: %s, id: %s'%(file1['title'],file1['id']))