Google云端硬盘API如何找到文件的路径? [英] Google Drive API How can I find the path of a file?

查看:611
本文介绍了Google云端硬盘API如何找到文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google云端硬盘API获取文件列表时,我正在尝试查找文件的路径.

I'm trying to find the path of a file when fetching a file list with the Google Drive API.

现在,我能够获取文件属性(当前仅获取校验和,id,名称和mimeType):

Right now, I'm able to fetch file properties (currently only fetching checksum, id, name, and 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()方法)

List documentation (parameters fed to the list() method)

文件文档(每个文件返回的属性) >

Files documentation (properties returned with each file)

推荐答案

  • 您要从自己的Google云端硬盘中的文件中检索文件夹树.
    • 您要检索文件路径.因此,在您的情况下,它将在每个文件和文件夹上方检索一个父文件夹.
      • You want to retrieve a folder tree from a file in own Google Drive.
        • You want to retrieve the file path. So in your case, it retrieves a parent folder above each file and folder.
        • 如果我的理解是正确的,那么该示例脚本如何?不幸的是,在当前阶段,Google 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. So it is required to prepare a script for achieving it. Please think of this as just one of several answers.

          此示例脚本检索文件的文件夹树.使用此脚本时,请设置文件ID.

          This sample script retrieves the folder tree of the file. When you use this script, please set the file 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.
            }
          ]
          

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆