获取 Google 已知路径文件的驱动器项目 ID

Get Google Drive item id of file with known path

文件位于 Google 驱动器上的已知路径中,例如:

/root/Myfiles/test.txt

如何使用 Google Drive V3 REST API (https://www.googleapis.com/drive/v3/files/) 获取文件的项目 ID?详细地说,我不确定如何为此构造查询参数q=

此致,

除非您有 MyFiles 的文件 ID,否则您将不得不在两次调用中执行此操作。

我们要做的第一件事是列出根目录中的所有目录。

这可以使用您已经知道的 Q 参数来完成

通过传递 parents in 'root' and mimeType = 'application/vnd.google-apps.folder' and name ='Myfiles' 我告诉它我正在寻找一个名为 Myfiles 的文件夹,该文件夹的父文件夹为 root。

curl \
  'https://www.googleapis.com/drive/v3/files?q=parents%20in%20%27root%27%20and%20mimeType%20%3D%20%27application%2Fvnd.google-apps.folder%27%20and%20name%20%3D%27YouTube%27&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

此响应将如下所示

{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "1R_QjyKyvET838G6loFSRu27C-3ASMJJa",
   "name": "Myfiles",
   "mimeType": "application/vnd.google-apps.folder"
  }
 ]
}

我知道名为 Myfiles 的文件夹的文件 ID

现在我可以做另一个调用,我在那个目录 id 中请求一个名为 test.txt 的文件,就像这样 parents in '1R_QjyKyvET838G6loFSRu27C-3ASMJJa' and name = 'test.txt'

代码看起来像这样

curl \
  'https://www.googleapis.com/drive/v3/files?q=parents%20in%20%271R_QjyKyvET838G6loFSRu27C-3ASMJJa%27%20and%20name%20%3D%20%27test.txt%27&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

回应

{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "1_BgrWKsjnZvayvr2kbdHzSzE3K2tNsWhntBsQwfrDOw",
   "name": "test.txt",
   "mimeType": "application/vnd.google-apps.document"
  }
 ]
}

总结

正如@DalmTo 所说,如果您想在特定文件夹中搜索文件,您需要拥有该 ID 才能在其中进行搜索。

parents in Whether the parent’s collection contains the specified ID.

这意味着您应该执行两个单独的查询。一个询问您的文件夹的 ID,另一个在该文件夹中寻找文件 test.txt

q: parents in "root" and mimeType = "application/vnd.google-apps.folder" and name = "Myfiles"

q: parents in "ID_FOLDER" and mimeType = "text/plain" and name = "test"

示例:

如果您的整个云端硬盘中只有一个文件符合要求的特征,您可以在一个查询中完成:

q: name = "test" and mimeType = "text/plain"

注意:如果您已上传文件,云端硬盘可能已将其检测为:application/octet-stream。通常 .txt 文件被检测为 plain/text,有关 MIME 类型和驱动器 API 的更多信息,您可以检查 here for common MIME types and here 以了解驱动器特定类型。

使用 Google Apps 脚本的替代方法

这是一个使用 Google Apps Script 的例子:

function findFile() {
  var folderId;
  var folderQuery = '"root" in parents and title = "Myfiles" and mimeType = "application/vnd.google-apps.folder"'
  let folder = Drive.Files.list({
    q: folderQuery
  })
  folderId = folder.items[0].id
  let fileQuery = `parents in "${folderId}" and title = "test"`
  var file = Drive.Files.list({
    q: fileQuery
  })
  return file.items[0].id
}

警告:Google Apps 脚本使用 Drive API v2,在这种情况下 query_term name 变为 title

更多信息

要更深入地了解驱动器 API 的工作原理,您可以查看 Search for files 指南:

A query string contains the following three parts:

query_term operator values

  • query_term is the query term or field to search upon.
  • operator specifies the condition for the query term.
  • values are the specific values you want to use to filter your search results

在客户端库之外使用时要牢记:

Note: These examples use the unencoded q parameter, where name = 'hello' is encoded as name+%3d+%27hello%27. Client libraries handle this encoding automatically.