简化Google Drive API的使用。
项目描述
PyDrive
PyDrive 是一个封装了 google-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只需两行即可完成。您可以在一个设置文件 settings.yaml 中自定义OAuth2的行为。
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = 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 file
file1.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/png
file3 = 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 query
file_list = drive.ListFile({'q': "'root' in parents"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
# Paginate file lists by specifying number of max results
for file_list in drive.ListFile({'maxResults': 10}):
print 'Received %s files from Files.list()' % len(file_list) # <= 10
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
并发访问变得简单
所有调用都是线程安全的。底层实现位于 google-api-client 库中 不是线程安全的,这意味着每个请求都必须重新认证 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-object。
项目详情
下载文件
下载您平台上的文件。如果您不确定该选择哪个,请了解有关 安装包 的更多信息。
源分发
PyDrive-1.3.1.tar.gz (987.4 kB 查看散列)
构建分发
PyDrive-1.3.1-py2-none-any.whl (25.7 kB 查看散列)
关闭
PyDrive-1.3.1.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 83890dcc2278081c6e3f6a8da1f8083e25de0bcc8eb7c91374908c5549a20787 |
|
MD5 | 99c51a87c22edaed718b1bc8d00058e9 |
|
BLAKE2b-256 | 52e00e64788e5dd58ce2d6934549676243dc69d982f198524be9b99e9c2a4fd5 |
关闭
PyDrive-1.3.1-py2-none-any.whl 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 5b94e971430722eb5c40a090f21df46b32e51399d747c1511796f63f902d1095 |
|
MD5 | b85a3e7e5963320a43778f35d68e9972 |
|
BLAKE2b-256 | 6b2dc8e052ba51099faee0bfe71d84f35bb1576e6910483cad46b840a122ca6c |