简体   繁体   English

如何找到谷歌驱动器的特定文件ID?

[英]How to find a particular file id of google drive?

In this code, I am finding a particular folder Id against its name.在这段代码中,我根据其名称找到了一个特定的文件夹 ID。 But I want to find a particular file id against its name.但我想根据其名称查找特定的文件 ID。 How to do it?怎么做?

def findId(self, filename):
    page_token = None
    while True:
        response = self.service.files().list(q="name = '"+ filename +"' and mimeType = 'application/vnd.google-apps.folder'",
                                                  spaces='drive',
                                                  fields='nextPageToken, files(id, name)',
                                                  pageToken=page_token).execute()
        for file in response.get('files', []):
            print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

Lets look at how the q parameter for the file.list method actually works.让我们看看 file.list 方法的 q 参数实际上是如何工作的。 This opitno lets you search on a lot of things, name is just one of them.这个 opitno 可以让你搜索很多东西,名字只是其中之一。

The first thing to remember is everything in Google drive is a file and it has a fileid.首先要记住的是 Google 驱动器中的所有内容都是一个文件,并且它有一个 fileid。 So your current search is searching for files with the name of filename and the internal google drive mime type of folder.因此,您当前的搜索正在搜索具有文件名名称和内部 google drive mime 文件夹类型的文件。

name = '"+ filename +"' and mimeType = 'application/vnd.google-apps.folder'",

You could then switch this to just search after name which would then return all the files with any mimetype that match that name.然后,您可以将其切换为仅在名称后搜索,然后返回具有与该名称匹配的任何 mimetype 的所有文件。

name = '"+ filename +"'"

You will then get a list back of all the files that match that name, the problem then being if you have more the one file on your drive account matching that name.然后,您将获得与该名称匹配的所有文件的列表,然后问题是您的驱动器帐户上是否有多个与该名称匹配的文件。

def findId(self, filename):
    page_token = None
    while True:
        response = self.service.files().list(q="name = '"+ filename +"'",
                                                  spaces='drive',
                                                  fields='nextPageToken, files(id, name)',
                                                  pageToken=page_token).execute()
        for file in response.get('files', []):
            print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

There is some documentation you might find interesting that goes into detail which options you can send with Q parameter search files I also have a video which explains it Google drive API V3: Beginners to listing files and searching files you may find the explantation helpful, the code itself is in C#.您可能会发现一些有趣的文档,其中详细介绍了您可以使用 Q 参数搜索文件发送哪些选项代码本身在 C# 中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM