在 Google 驱动器中查找文件的上一个父文件夹

Find file's previous parent folder in Google Drive

假设我有文件夹 A 和文件夹 B。File.txt 存储在文件夹 A 中并移动到文件夹 B。 给定文件,是否可以知道文件的前一个父文件夹? 似乎可以知道文件已移动到的 文件夹,但不知道原始文件夹。

您可以使用 Google Drive Activity API 来检索有关用户 Google 驱动器中所做更改的信息。

使用activity.query()

Making Requests in the Drive Activity API

有两种请求方式activity:通过云端硬盘项目,或文件夹层次结构下的所有内容。

  • itemName:此键的格式为“items/ITEM_ID”。通常这是云端硬盘中的一个文件。如果您为此键指定一个文件夹,它将显示文件夹本身的 activity,例如文件夹创建或重命名的时间。

  • ancestorName:此键的格式为“items/FOLDER_ID”,响应将包含此文件夹下子树中所有项目的 activity。

当没有设置键时,它默认使用祖先名称“items/root”,这将为您的 Google 驱动器中的所有项目显示 activity。

Filtering

您可以通过在请求中构造 filter 字符串来限制可能在 DriveActivity 对象中 return 编辑的操作。

要按操作类型进行限制,请使用带有“has”运算符 (:) 的字段名称 detail.action_detail_case 以及括在括号中的单值或允许的操作类型列表。示例包括:

detail.action_detail_case: RENAME
detail.action_detail_case:(CREATE UPLOAD)
-detail.action_detail_case:MOVE

这些过滤条件可以合并到一个过滤字符串中。


示例请求正文:

{
  "filter": "detail.action_detail_case:MOVE",
  "itemName": "items/1kNGhKfVBtNHDNZPxUEzHYxxxxxx"
}
  • 过滤所有具有 move ActionDetail
  • 的驱动器 activity
  • 主要标准是 return 具有 file id: 1kNGhKfVBtNHDNZPxUEzHYxxxxxx
  • 的特定文件的活动

响应正文:

这将 return 一个 DriveActivity object that will contain a Move object under ActionDetail 对象。您可以参考 removedParents->driveItem 来获取有关文件的上一个父文件夹的信息。

{
  "activities": [
    {
      "primaryActionDetail": {
        "move": {
          "addedParents": [
            {
              "driveItem": {
                "name": "items/1TrX6KcAJppWCj9GSUjSYn79Aqxxxx",
                "title": "NewFolder",
                .....
              }
            }
          ],
          "removedParents": [
            {
              "driveItem": {
                "name": "items/1YUrD6lUshY2IG0fIi0aFUoQRxxxx",
                "title": "Untitled folder",
                .....
              }
            }
          ]
        }
      },
      ......
      ],
      "actions": [
        {
          ......
        }
      ],
      "targets": [
        {
          "driveItem": {
            "name": "items/1kNGhKfVBtNHDNZPxUEzHYxxxxxx",
            "title": "sampledoc.json",
            ......
          }
        }
      ],
      "timestamp": "2021-03-16T16:04:24.072Z"
    }
  ]
 }