相关疑难解决方法(0)

Python Google Drive API - 列出整个驱动器文件树

我正在构建一个使用Google驱动器API的python应用程序,所以开发很好,但是我有一个问题需要检索整个Google驱动器文件树,我需要它有两个目的:

  1. 检查路径是否存在,所以如果我想在root/folder1/folder2下上传test.txt我想检查文件是否已经存在并在案例中更新它
  2. 构建一个可视化文件浏览器,现在我知道谷歌提供了他自己的(我现在不记得名字,但我知道存在)但我想将文件浏览器限制为特定的文件夹.

现在我有一个获取Gdrive根目录的函数,我可以通过递归调用一个函数来构建三个函数,这个函数列出了我单个文件夹的内容,但它非常慢并且可能会向Google发出数千个请求,这是不能接受的.

这里获取root的函数:

def drive_get_root():
"""Retrieve a root list of File resources.
Returns:
List of dictionaries.
"""

#build the service, the driveHelper module will take care of authentication and credential storage
drive_service = build('drive', 'v2', driveHelper.buildHttp())
# the result will be a list
result = []
page_token = None
while True:
    try:
        param = {}
        if page_token:
            param['pageToken'] = page_token
        files = drive_service.files().list(**param).execute()
        #add the files in the list
        result.extend(files['items'])
        page_token = files.get('nextPageToken')
        if not page_token:
            break
    except …
Run Code Online (Sandbox Code Playgroud)

python google-api google-drive-api

7
推荐指数
2
解决办法
9153
查看次数

标签 统计

google-api ×1

google-drive-api ×1

python ×1