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

问题描述:

我刚开始使用适用于Python(v3)的Google Drive API,并且一直在尝试访问和更新我具有fileId的特定父文件夹中的子文件夹.这是我为API驱动程序构建的版本:

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. Here is my build for the API driver:

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

我能够使用 files().list()成功访问子文件夹的大部分,但返回的结果列表中至少缺少一个:

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:

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

我仔细检查了一下,结果中没有 nextPageToken 键,并且 incompleteSearch 的值是 False ,我认为这意味着完整返回结果列表.另外,当我使用 file().get()方法访问丢失文件的父级列表时,列出的唯一父级是上面查询中的父级:

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. 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:

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

并返回以下内容:

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

其他可能相关的详细信息:

Other details that may be relevant:

  • 此未出现在列表中的特定文件夹已由协作者重命名.
  • 我正在jupyter笔记本上运行此代码,而不是从python文件中运行.
  • 我是一位具名的协作者,对所有子文件夹(包括未显示的子文件夹)都具有写访问权.

更新

  • files().list()查询用于返回文件夹中41条记录的40条记录.现在只返回39.
  • 这两个不再返回的文件夹都由使用扩展了写级别权限的链接访问该文件夹的人重命名.
  • 使用 files().get()直接查询其文件夹详细信息时,两个未返回的文件夹仍将其父文件夹作为其唯一父文件夹,并且其权限没有更改./li>
  • The files().list() query used to return 40 records of the 41 in the folder. Now it is only returning 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.

主要问题:

  1. 为什么在我的 file().list()查询中没有明确列出父ID的文件显示在该查询的结果中?并有什么方法可以调整查询或文件以确保它能做到吗?
  2. 是否有一种简便的方法来列出Google Drive API v3的文件夹中包含的所有文件?我知道v2具有用于文件夹的 children()方法,但是据我所知在v3中已弃用该方法
  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? 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? I know that v2 had a children() method for folders, but it's been deprecated in v3 to my knowledge

我用我的代码找出了错误:

I figured out the error with my code:

我在 files().list()方法中的上一个查询参数是:

My previous query parameter in the files().list() method was:

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

在查看了有人在Google的问题跟踪器中针对该API发布的另一个错误之后,我看到了该查询的首选语法是:

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:

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

换句话说,将文件ID 中的 parents的顺序切换为父母信息 中的 fileId的顺序.随着语法的更改,所有41个文件都被返回.

In other words switching the order of parents in fileId to fileId in parents. With the resulting change in syntax all 41 files were returned.

我有两个后续问题,希望有人可以澄清:

I have two follow-up questions that hopefully someone can clarify:

  1. 如果第一种语法不正确,为什么会完全返回任何记录?为什么更改文件名会阻止使用第一种语法返回文件?
  2. 如果您想返回存储在几个文件夹之一中的文件列表,有什么方法可以像 parents ... 语法那样将多个父ID传递给查询.建议?还是必须将它们作为单独的条件进行评估,即父母中的 fileId1或父母中的fileId2 ?
  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? Or do they have to be evaluated as separate conditions i.e. 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.