跳转到主要内容

让Google Drive API变得简单。PyDrive的维护分支。

项目描述

GHA Tests Conda-forge PyPI

PyDrive2

PyDrive2 是一个包装库,简化了Google Drive API V2的许多常见任务。它是 google-api-python-client 的包装库。是 https://pypi.python.org/pypi/PyDrive 的一个活跃维护分支。由Git for Data - DVC项目的作者和维护者维护。

项目信息

PyDrive2的功能

  • 使用灵活的设置将OAuth2.0简化为几行代码。

  • 将Google Drive API V2包装成每个资源的类,使您的程序更具面向对象性。

  • 帮助执行除API调用之外的一般操作,例如内容获取和分页控制。

  • 提供fsspec文件系统实现。

如何安装

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

$ pip install PyDrive2

要从GitHub安装当前的开发版本,使用以下命令

$  pip install git+https://github.com/iterative/PyDrive2.git#egg=PyDrive2

OAuth变得简单

从Google API控制台和OAuth2.0下载client_secrets.json,两行代码即可完成OAuth2.0。您可以在一个设置文件settings.yaml中自定义OAuth2的行为。

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

文件管理变得简单

使用一个方法上传/更新文件。PyDrive2将以最有效的方式完成此操作。

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')

文件列表分页变得简单

PyDrive2为您处理文件列表分页。

# 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: {}, id: {}'.format(file1['title'], file1['id']))

# Paginate file lists by specifying number of max results
for file_list in drive.ListFile({'maxResults': 10}):
    print('Received {} files from Files.list()'.format(len(file_list))) # <= 10
    for file1 in file_list:
        print('title: {}, id: {}'.format(file1['title'], file1['id']))

Fsspec文件系统

PyDrive2通过fsspec兼容的GDriveFileSystem提供轻松处理文件的方式。

from pydrive2.fs import GDriveFileSystem

# replace `root` with ID of a drive or directory and give service account access to it
fs = GDriveFileSystem("root", client_id=my_id, client_secret=my_secret)

for root, dnames, fnames in fs.walk("root"):
    ...

并发访问变得简单

所有API函数均设计为线程安全。

贡献者

感谢所有贡献者!

https://contrib.rocks/image?repo=iterative/PyDrive2

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

pydrive2-1.20.0.tar.gz (63.2 kB 查看哈希值)

上传时间

构建分发

PyDrive2-1.20.0-py3-none-any.whl (47.9 kB 查看哈希值)

上传时间 Python 3

由以下支持