简体   繁体   English

Google Drive API如何找到文件的路径?

[英]Google Drive API How can I find the path of a file?

I'm trying to find the path of a file when fetching a file list with the Google Drive API. 使用Google云端硬盘API提取文件列表时,我正在尝试查找文件的路径。

Right now, I'm able to fetch file properties (currently only fetching checksum, id, name, and mimeType): 现在,我能够获取文件属性(当前仅获取校验和,ID,名称和mimeType):

results = globalShares.service.files().list(pageSize=1000,corpora='user',fields='nextPageToken, files(md5Checksum, id, name, mimeType)').execute()
items = results.get('files',[])
nextPageToken = results.get('nextPageToken',False)
for file in items:
    print("===========================================================")
    pp.pprint(file)
print(str(len(items)))
print(nextPageToken)

List documentation (parameters fed to the list() method) 列表文档 (提供给list()方法的参数)

Files documentation (properties returned with each file) 文件文档 (每个文件返回的属性)

  • You want to retrieve a folder tree from a file in own Google Drive. 您要从自己的Google云端硬盘中的文件中检索文件夹树。
    • You want to retrieve the file path. 您要检索文件路径。 So in your case, it retrieves a parent folder above each file and folder. 因此,在您的情况下,它将在每个文件和文件夹上方检索一个父文件夹。
  • You want to achieve this using google-api-python-client with python. 您想使用带有python的google-api-python-client实现此目的。
  • You have already been able to get the file metadata using Drive API. 您已经能够使用Drive API获取文件元数据。

If my understanding is correct, how about this sample script? 如果我的理解是正确的,那么该示例脚本如何? Unfortunately, in the current stage, the folder tree of the file cannot directly be retrieved by the Google API. 不幸的是,在当前阶段,Google API无法直接检索文件的文件夹树。 So it is required to prepare a script for achieving it. 因此,需要准备一个脚本来实现它。 Please think of this as just one of several answers. 请认为这只是几个答案之一。

Sample script: 示例脚本:

This sample script retrieves the folder tree of the file. 此示例脚本检索文件的文件夹树。 When you use this script, please set the file ID. 使用此脚本时,请设置文件ID。

fileId = '###'  # Please set the file ID here.

tree = []  # Result
file = globalShares.service.files().get(fileId=fileId', fields='id, name, parents').execute()
parent = file.get('parents')
if parent:
    while True:
        folder = service.files().get(
            fileId=parent[0], fields='id, name, parents').execute()
        parent = folder.get('parents')
        if parent is None:
            break
        tree.append({'id': parent[0], 'name': folder.get('name')})

print(tree)

Result: 结果:

In the case that the file has the three-layer structure, when you run the script, the following object is returned. 如果文件具有三层结构,则在运行脚本时,将返回以下对象。

[
  {
    "id": "folderId3",
    "name": "folderName3"
  },
  {
    "id": "folderId2",
    "name": "folderName2"
  },
  {
    "id": "folderId1",
    "name": "My Drive"  # This is the root folder.
  }
]
  • The 1st element is the bottom layer. 第一个元素是底层。

Note: 注意:

  • In this script, from OP's situation which want to retrieve "the file path", it supposes that each file has only one parent. 在此脚本中,从OP想要检索“文件路径”的情况出发,假设每个文件只有一个父文件。 At the file system of Google Drive, each file can have multiple parents. 在Google云端硬盘的文件系统中,每个文件可以有多个父级。 If in your situation, there are several files which have the multiple parents, this script returns the 1st element of the array of parents . 如果在您的情况下,有多个文件具有多个父对象,则此脚本将返回parents数组的第一个元素。 Please be careful this. 请注意这一点。 This is also mentioned by pinoyyid's comment . pinoyyid的评论也提到了这一点。

Reference: 参考:

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

相关问题 如何在 Google Drive API 中按路径查找文件夹 ID? - How to find folder ID by path in Google Drive API? 如何使用Google Drive API将文件传输到其他用户的Drive? - How can I use the Google Drive API to transfer a file to another user's Drive? 如何通过 python 中安装的谷歌驱动器上文件的路径获取文件/文件夹的共享链接? - How can I get share link of file/folder by path to the file on insalled google drive in python? 如何通过google drive api在python的google drive中查找当前日期修改的文件和用户名? - How to find modified file and user name on current date in google drive in python by google drive api? 谷歌驱动器 API:如何找到我与他人共享的所有文件 - Google drive API : How to find all files I shared with others Google Drive API无法找到任何文件夹 - Google Drive API can not find any folder 如何在Google Drive API v3中获取文件的“lastModified”? - How can I get file's 'lastModified' in Google Drive API v3? 如何通过google驱动器的链接找到文件的类型? - How do I find the type of the file through the link of the google drive? 如何查找Google云端硬盘文件夹中的内容是否已更改 - How can I find if the contents in a Google Drive folder have changed 如何在 Python 中使用 Google Drive API 创建新文件夹? - How can I create a new folder with Google Drive API in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM