google drive api很简单。

PyDrive的Python项目详细描述


PyDrive

pydrivegoogle-api-python-client 这简化了许多常见的google drive api任务。

PyDrive的功能

  • 将OAuth2.0简化为具有灵活设置的几行。
  • Google Drive API包装到 每个资源的类,使程序更面向对象。
  • 帮助除api调用以外的常见操作,例如内容获取 以及分页控制。

如何安装

可以使用常规的pip命令安装pydrive。

$ pip install PyDrive

要从github安装当前的开发版本,请使用:

$  pip install git+https://github.com/googledrive/PyDrive.git#egg=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']))

并发访问变得简单

所有呼叫都是线程安全的。中的底层实现 google api客户端库 is not thread-safe, 这意味着每个请求都必须重新验证一个http对象。你 可以通过 为每个线程创建自己的http对象,并为每个调用重用它。

具体操作如下:

# Create httplib.Http() object.http=drive.auth.Get_Http_Object()# Create file object to upload.file_obj=drive.CreateFile()file_obj['title']="file name"# Upload the file and pass the http object into the call to Upload.file_obj.Upload(param={"http":http})

您可以在每个采用param的访问方法中指定http对象。 参数。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
oop Java多态性:如何指示应该使用子类进行比较?   JavaLibGDX:渲染基于可缩放向量的形状的最快方法是什么?   java如何通过可选选项同时检查NPE上对象的字段和条件?如果一切正常,什么都不做,否则抛出异常   java使用camera2获取图像捕获时间   未单击时选中java Recyclerview复选框   java Android Hello world项目未运行   java在网站上发布处理代码?   java如何在jdialog中设置垂直滚动条?   java等待并通知   使用JavaFX在raspberry PI上构建java Maven失败   java处理空值Hazelcast投影   在java中为以下数据选择完美的数据结构   通过ApacheHttpClient将对象的arraylist从Java传递到PHP   使用同步关键字的多线程java等待通知方法   java素数与布尔逻辑   java组织。json。JSONException:JSONArray文本必须以“[”开头