简体   繁体   English

Google Drive API v3 Python files()。list()中缺少某些文件

[英]Certain files missing in Google Drive API v3 Python files().list()

I'm new to using the Google Drive API for Python (v3) and I've been trying to access and update the sub-folders in a particular parent folder for which I have the fileId. 我是使用适用于Python(v3)的Google Drive API的新手,并且一直在尝试访问和更新我具有fileId的特定父文件夹中的子文件夹。 Here is my build for the API driver: 这是我为API驱动程序构建的:

store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json',
           scope='https://www.googleapis.com/auth/drive')
    creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))

I am able to successfully access most of the sub-folders by using files().list() but at least one was missing from the list of results returned: 我可以通过使用files().list()成功访问大多数子文件夹,但返回的结果列表中至少缺少一个子文件夹:

results = service.files().list(
    q="parents in '1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw'", 
    fields="files(id, name), incompleteSearch, nextPageToken").execute()
items = results['files']

I double checked and there was no nextPageToken key in the results and the value of incompleteSearch was False , which I assume means the full list of results were returned. 我仔细检查了一下,结果中没有nextPageToken键, incompleteSearch的值为False ,我认为这意味着将返回完整的结果列表。 In addition when I accessed the list of parents for the missing file by using the file().get() method, the only parent listed is the one in the query above: 另外,当我使用file().get()方法访问丢失文件的父级列表时,列出的唯一父级是上面查询中的父级:

service.files().get(
    fileId='1WHP02DtXfJHfkdr47xSeeRIj0sCrihPA',
    fields='parents, name').execute()

and returns this: 并返回此:

{'name': 'Sara Gaul -Baltimore Corps docs and schedules',
 'parents': ['1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw']}

Other details that may be relevant: 其他可能相关的细节:

  • This particular folder that is not appearing in the list was renamed by a collaborator. 这个没有出现在列表中的特定文件夹被协作者重命名。
  • I'm running this code on a jupyter notebook instead from a python file. 我在jupyter笔记本上而不是从python文件运行此代码。
  • I'm a named collaborator with write access on all of the sub-folders, including the one that's not showing up. 我是一个具名的协作者,对所有子文件夹都具有写访问权,包括未显示的子文件夹。

UPDATES 更新

  • The files().list() query used to return 40 records of the 41 in the folder. files().list()查询用于返回文件夹中41条记录的40条记录。 Now it is only returning 39. 现在只返回39。
  • Both of the folders that are no longer being returned were renamed by someone who accessed the folder using the link that extends write level permissions. 通过使用扩展了写级别权限的链接访问该文件夹的人,已将不再返回的两个文件夹重命名。
  • When their folder details are queried directly using files().get() both of the non-returned folders still have the parent folder as their only parent, and their permissions have not changed. 当直接使用files().get()查询其文件夹详细信息时,两个未返回的文件夹仍将其父文件夹作为其唯一的父文件夹,并且它们的权限没有更改。

Main questions: 主要问题:

  1. Why isn't this file which clearly has the parent id listed in my file().list() query showing up in the results of that query? 为什么在我的file().list()查询中显然没有列出父ID的file().list()显示在该查询的结果中? And is there any way to adjust the query or the file to ensure that it does? 是否有任何方法可以调整查询或文件以确保执行此操作?
  2. Is there an easier way to list all of the files contained within a folder in the Google Drive API v3? 有没有一种简便的方法可以列出Google Drive API v3的文件夹中包含的所有文件? I know that v2 had a children() method for folders, but it's been deprecated in v3 to my knowledge 我知道v2有用于文件夹的children()方法,但据我所知在v3中已弃用

I figured out the error with my code: 我用我的代码找出了错误:

My previous query parameter in the files().list() method was: 我在files().list()方法中的上一个查询参数是:

results = service.files().list(
    q="parents in '1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw'", 
    fields="files(id, name), incompleteSearch, nextPageToken").execute()
items = results['files']

After looking at another bug someone had posted in Google's issue tracker for the API, I saw the preferred syntax for that query was: 在查看了有人在Google的问题跟踪器中针对该API发布的另一个错误之后,我看到了该查询的首选语法是:

results = service.files().list(
    q="'1QXPl6z04GsYAO0GKHBk2oBjEweaAbczw' in parents", 
    fields="files(id, name), incompleteSearch, nextPageToken").execute()
items = results['files']

In other words switching the order of parents in fileId to fileId in parents . 换句话说parents in fileIdfileId in parents顺序切换为parents in fileId中的fileId in parents With the resulting change in syntax all 41 files were returned. 随着语法的更改,返回了所有41个文件。

I have two follow-up questions that hopefully someone can clarify: 我有两个后续问题,希望有人可以澄清:

  1. Why would the first syntax return any records at all if it is incorrect? 如果第一种语法不正确,为什么会根本返回任何记录? And why would changing the name of a file prevent it from being returned using the first syntax? 为什么更改文件名会阻止使用第一种语法返回文件?
  2. If you wanted to return a list of files that were stored in one of a few folders, is there any way to pass multiple parent ids to the query as the parents in ... syntax would suggest? 如果您想返回存储在几个文件夹之一中的文件的列表,是否有任何方法可以将多个父ID传递给查询,如parents in ...语法中的parents in ...所建议的那样? Or do they have to be evaluated as separate conditions ie fileId1 in parents or fileId2 in parents ? 还是必须将它们作为单独的条件进行评估,即fileId1 in parents or fileId2 in parents

If someone could comment on this answer with those explanations or post a more complete answer, I would gladly select it as the best response. 如果有人可以用这些解释对这个答案发表评论或发布更完整的答案,我很乐意将其选择为最佳答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google Drive API v3获取文件的大小 - Get size of files with Google Drive API v3 Google Drive API v3:service.files().list 不返回所有文件夹 - Google Drive API v3: service.files().list not returning all folders 尝试从Google Drive Api v3获取文件列表时出现编码错误 - Encoding error when trying to obtain list of files from Google Drive Api v3 如何使用 Python 将文件插入/上传到 Google Drive API v3 中的特定文件夹 - How can I insert/upload files to a specific folder in Google Drive API v3 using Python 无法与共享驱动器和文件交互 | Python 谷歌驱动器 API v3 - Cannot Interact with Shared Drives & Files | Python Google Drive API v3 Drive API v3 - 获取特定文件夹中的所有文件 - Drive API v3 - Getting all files inside a particular folder 如何使用 V3 API Python 客户端按 ID 列出 Google Drive 文件夹的内容? - How to list the contents of a Google Drive folder by ID with the V3 API Python client? Google Drive API v3 files.export 方法抛出 403 错误:“Export 仅支持 Docs Editors 文件。” - Google Drive API v3 files.export method throws a 403 error: “Export only supports Docs Editors files.” 如何使用 Python (FLASK) 实现 Google Drive V3 API - How to Implement Google Drive V3 API with Python (FLASK) 如何在 Python 中使用 Google Drive API v3 更改所有者? - How to change the owner with Google Drive API v3 in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM