选择特定字段 Google Drive API v3

Selecting specific fields Google Drive API v3

我终于得到了 Google Drive API V3 来使用服务帐户。

现在要从驱动器中检索所有文件,我使用以下命令:

$optParams = [
    'corpora' => 'drive',
    'driveId' => env('GOOGLE_DRIVE_ID'),
    'includeItemsFromAllDrives' => true,
    'supportsAllDrives' => true,
    'fields' => 'files(name,mimeType,trashed,parents,version,webContentLink,webViewLink,createdTime,modifiedTime,size)'
];

$this->googleDrive->files->listFiles($optParams);

所以我专门要求文件数组中的某些字段。问题是,所有其他字段 仍然存在(除了它们都是 null 值)。这是正常行为吗?因为如果我试图检索 20 到 50 个文件,仍然有一些无用的 Kb 正在传输。

响应示例:

  +"files": array:2 [▼
    0 => Google_Service_Drive_DriveFile {#279 ▼
      #collection_key: "spaces"
      +appProperties: null
      #capabilitiesType: "Google_Service_Drive_DriveFileCapabilities"
      #capabilitiesDataType: ""
      #contentHintsType: "Google_Service_Drive_DriveFileContentHints"
      #contentHintsDataType: ""
      +copyRequiresWriterPermission: null
      +createdTime: "2019-05-22T11:41:25.852Z"
      +description: null
      +driveId: null
      +explicitlyTrashed: null
      +exportLinks: null
      +fileExtension: null
      +folderColorRgb: null
      +fullFileExtension: null
      +hasAugmentedPermissions: null
      +hasThumbnail: null
      +headRevisionId: null
      +iconLink: null
      +id: null
      #imageMediaMetadataType: "Google_Service_Drive_DriveFileImageMediaMetadata"
      #imageMediaMetadataDataType: ""
      +isAppAuthorized: null
      +kind: null
      #lastModifyingUserType: "Google_Service_Drive_User"
      #lastModifyingUserDataType: ""
      +md5Checksum: null
      +mimeType: "application/zip"
      +modifiedByMe: null
      +modifiedByMeTime: null
      +modifiedTime: "2019-05-22T11:41:25.852Z"
      +name: "<something>"
      +originalFilename: null
      +ownedByMe: null
      #ownersType: "Google_Service_Drive_User"
      #ownersDataType: "array"
      +parents: array:1 [▶]
      +permissionIds: null
      #permissionsType: "Google_Service_Drive_Permission"
      #permissionsDataType: "array"
      +properties: null
      +quotaBytesUsed: null
      +shared: null
      +sharedWithMeTime: null
      #sharingUserType: "Google_Service_Drive_User"
      #sharingUserDataType: ""
      +size: "455778"
      +spaces: null
      +starred: null
      +teamDriveId: null
      +thumbnailLink: null
      +thumbnailVersion: null
      +trashed: false
      +trashedTime: null
      #trashingUserType: "Google_Service_Drive_User"
      #trashingUserDataType: ""
      +version: "2"
      #videoMediaMetadataType: "Google_Service_Drive_DriveFileVideoMediaMetadata"
      #videoMediaMetadataDataType: ""
      +viewedByMe: null
      +viewedByMeTime: null
      +viewersCanCopyContent: null
      +webContentLink: "<something>"
      +webViewLink: "<something>"
      +writersCanShare: null
      #internal_gapi_mappings: []
      #modelData: []
      #processed: []
    }
    1 => Google_Service_Drive_DriveFile {#269 ▶}

google 驱动器 api v3 实现了一个名为 Partial response actually most google apis have this fields 的东西,是一个可选参数。

By default, the server sends back the full representation of a resource after processing requests. For better performance, you can ask the server to send only the fields you really need and get a partial response instead.

它的 IMO 并不是每一个都有很好的记录,因为上面的陈述是正确的。

Drive v3 默认情况下 NOT 发回完整的表示。这是驱动器 v3 的主要区别,其他 apis 通常默认为 return 所有内容,并且仅在开发人员使用字段参数请求时才进行部分响应。

驱动器 files.list 响应包含一个文件列表,它实际上只会默认 return 以下 4 个字段。

{
   "kind": "drive#file",
   "id": "hzqXfMiOiFlrYdQCx3Rram0vuf9lmXa",
   "name": "Sayak",
   "mimeType": "application/vnd.google-apps.folder"
  }

您看到的空值实际上可能来自您正在使用的将空对象值解析为空值的库。

如果你做

$optParams = [
    'corpora' => 'drive',
    'driveId' => env('GOOGLE_DRIVE_ID'),
    'includeItemsFromAllDrives' => true,
    'supportsAllDrives' => true,
    'fields' => '*'
];

它实际上会为您填写所有字段。