我正在尝试获取 Google Drive API php 脚本中文件对象的父文件夹。
相关代码:
$idChangeHistory = [] ;
do {
$response = $service->changes->listChanges($nextPageToken, [ 'spaces' => 'drive' ] ) ;
if ( (! is_null($response->newStartPageToken))) {
$savedStartPageToken = $response->newStartPageToken ;
$historyRow['nextStartPageToken'] = $savedStartPageToken ;
}
// I collect the change IDs because there may be multiple changes per file.
// We don't care - we only want the final one. But we'll count the revisions.
foreach ($response->changes as $change) {
$googleId = $change->fileId ;
if (array_key_exists($googleId, $idChangeHistory))
$idChangeHistory[$googleId]['count']++ ;
else
$idChangeHistory[$googleId] = [ 'count' => 1 ] ;
}
$nextPageToken = $response->nextPageToken ;
} while ($nextPageToken) ;
foreach ($idChangeHistory as $googleId => $elements) {
$fileObj = $service->files->get($googleId) ;
$mimeType = $fileObj->getMimeType() ;
$name = $fileObj->getName() ;
$parents = $fileObj->getParents() ; // This comes up null ??????
$size = $fileObj->getSize() ;
$trashed = $fileObj->getTrashed() ;
$createdTime = $fileObj->getCreatedTime() ;
$modifiedTime = $fileObj->getModifiedTime() ;
// Somehow we need to get Folder name and parentId.
// (We can get the parent ID from DB if we can obtain folder name)
// (unless folder is new?)
$something = processEntry($fileObj, $folderName, $countsArray, $parentId) ;
}
...但我发现这些字段中的大多数实际上并未填充值 - 它们被设置为空。特别是, $obj->getParentd() 返回 null,我期望的是一个数组。使用上面的代码,仅填充 $mimeType 和 $name。所有其他值都为 null。
我知道 $service->files->listFiles() 允许使用“fields”参数,并且(在大多数情况下)只有那些提到的字段实际上会被填充。例如:
$result = $service->files->listFiles(
[ 'fields' => 'files(id,name,mimeType,fileExtension,size,parents,createdTime,modifiedTime,trashed),nextPageToken'
, 'q' => sprintf('"%s" in parents', $myGoogleId)
] ;
但似乎 $service->files->get($id) 不提供此功能。
如何获取填充的文件对象,或者获取以此方式检索的文件对象的父 ID?
谢谢。
虽然我不确定是否能正确理解你的预期结果,但是下面的修改怎么样?
$fileObj = $service->files->get($googleId) ;
$fileObj = $service->files->get($googleId, ["fields" => "*"]);
或者,
$fileObj = $service->files->get($googleId, ["fields" => "mimeType,size,name,parents,trashed,createdTime,modifiedTime"]);
fields
。通过此修改,将返回 parents
。