Google Drive API列表方法是递归的吗?

时间:2017-09-15 09:53:54

标签: python google-drive-api

juste一个简单的专业用途问题。你能告诉我驱动API v2的列表方法是递归的吗?如果它搜索GDrive帐户中存在的所有文件?

我的代码示例(Python):

def getUserFiles(service, parent):
try:
    results = []
    page_token = None
    while True:
        param = {}
        if page_token:
            param['pageToken'] = page_token
        param['q'] = '"me" in owners'
        if parent:
            param['q'] += " and '%s' in parents" % parent
        param['maxResults'] = 500
        param['fields'] = 'nextPageToken, items(id, mimeType)'
        files = service.files().list(**param).execute()
        results.extend(files['items'])
        page_token = files.get('nextPageToken')
        if not page_token:
            break
    return results
except Exception as e:
    logging.error('Error getting user\'s files : %s' % e)

我也加了这个:

files = getUserFiles(driveService, args.parent)
logging.info('Get childrens for each folders')
for f in files:
    if f['mimeType'] == 'application/vnd.google-apps.folder':
        files.extend(getUserFiles(driveService, f['id']))

但我想知道如果list方法返回所有文件是否无用......

谢谢! (抱歉我的英语不好)

1 个答案:

答案 0 :(得分:0)

不要让你的观点“递归”,但Drive API中的Files.list应该返回所有文件。另外,我建议您使用Files.list v3

如果您只想列出文件夹,请使用queries,如:

results = service.files().list(
        q="mimeType = 'application/vnd.google-apps.folder'"
        pageSize=10,
        fields="nextPageToken,
         files(id, name)")
         .execute()
    items = results.get('files', [])

该代码来自Python Quickstart