【发布时间】:2021-11-09 22:04:03
【问题描述】:
使用 Google API v3 我尝试将文件从一个文件夹移动到另一个文件夹。我在 Laravel 中使用包装类,文件和父 ID 是正确的。从文档开发,我尝试了以下代码:
public function moveFileFromTo($fileID, $toParentID) {
$fromFile = $this->service->files->get($fileID, ['fields' => '*']);
$fromParentID = $fromFile->getParents();
$blankFile = new Google_Service_Drive_DriveFile();
$this->service->files->update($fileID, $blankFile, [
"removeParents" => $fromParentID,
"addParents" => $toParentID
]);
}
但是,这似乎会移动文件,但会删除所有元数据。 我也试过了
public function moveFileFromTo($fileID, $toParentID) {
$fromFile = $this->service->files->get($fileID, ['fields' => '*']);
$fromParentID = $fromFile->getParents();
$fromFile->setParents($fromParentID);
$this->service->files->update($fileID, $fromFile);
}
但是,这会产生错误:
Google\Service\Exception
{ "error": { "errors": [ { "domain": "global", "reason":
"fieldNotWritable", "message": "The resource body includes fields
which are not directly writable." } ], "code": 403, "message": "The
resource body includes fields which are not directly writable." } }
我希望简单地移动文件并保留其所有元数据。从文档中,似乎更新中需要一个新的空文件(真的很奇怪),或者我必须以某种方式删除它不喜欢写入的第二个参数($fromFile)中使用的对象的字段(即使我只是在更新父母的文件——这是可写的)。
另见https://issuetracker.google.com/issues/199921300
到目前为止的答案问题: 但感谢回复
$fromFile = $this->service->files->get($fileID, ['fields' => 'parents, id']);
返回所有 ~75 个属性,其中很多是不可写的。 而不是根据 PHPStorm 调试预期的 2(请注意,中断位于 GET 请求之后的语句中,因此此时无关紧要
使用
unset($fromFile->shared);
仍然保留其他可写属性
确实该文件实际上并未共享
更新我的编码
public function moveFileFromTo($fileID, $toParentID) {
$fromFile = $this->service->files->get($fileID, ["fields" => "id,parents"]);
$fromFile = $this->getParsedWritableFile($fromFile);
$fromFile->setParents($toParentID);
$this->service->files->update($fileID, $fromFile, ['addParents' => $toParentID]);
}
getParsedWritableFile 正在尝试在新的 Google Drive 文件对象上设置可写属性:
public function getParsedWritableFile($gdrivefile) {
$gdrivefile = new \Google_Service_Drive_DriveFile();//comment or delete, just here to check auto complete function names
$parsedFile = new \Google_Service_Drive_DriveFile();
//$parsedFile=$gdrivefile;
// currently only allow these according to https://developers.google.com/drive/api/v3/reference/files#resource-representations
$parsedFile->setName($gdrivefile->getName());//name
$parsedFile->setMimeType($gdrivefile->getMimeType());//mimeType
$parsedFile->setDescription($gdrivefile->getDescription());//description
$parsedFile->setStarred($gdrivefile->getStarred());//starred
$parsedFile->setTrashed($gdrivefile->getTrashed());//trashed
$parsedFile->setParents($gdrivefile->getParents());//parents
$parsedFile->setProperties($gdrivefile->getProperties());//properties [object]
$parsedFile->setAppProperties($gdrivefile->getAppProperties());//appProperties [object]
$parsedFile->setCreatedTime($gdrivefile->getCreatedTime());//createdTime
$parsedFile->setModifiedTime($gdrivefile->getModifiedTime());//modifiedTime
$parsedFile->setWritersCanShare($gdrivefile->getWritersCanShare());//writersCanShare
$parsedFile->setViewedByMeTime($gdrivefile->getViewedByMeTime());//viewedByMeTime
$parsedFile->setFolderColorRgb($gdrivefile->getFolderColorRgb());//folderColorRgb
$parsedFile->setOriginalFilename($gdrivefile->getOriginalFilename());//originalFilename
$parsedFile->setCopyRequiresWriterPermission($gdrivefile->getCopyRequiresWriterPermission());//copyRequiresWriterPermission
/*complex permissions*/
/*
contentHints.thumbnail.image
contentHints.thumbnail.mimeType
contentHints.indexableText
*/
$contenthints=$gdrivefile->getContentHints();//could be null meaning further functions eg getThumbnail cause exception
if($contenthints){
$parsedFile->setContentHints($contenthints->getThumbnail()->getImage());
$parsedFile->setContentHints($contenthints->getThumbnail()->getMimeType());
$parsedFile->setContentHints($contenthints->getIndexableText());
}
/*no function to get indiviual attributes*/
/*
contentRestrictions[].readOnly
ccontentRestrictions[].reason
*/
$parsedFile->setContentRestrictions($gdrivefile->getContentRestrictions());
//</ end>
return $parsedFile;
}
上面的代码确实移动了它,看似正确的元数据、创建时间和 EXIF 数据现在完好无损
【问题讨论】:
-
您可能想尝试将文件复制到新位置,然后从原始位置删除文件。这可能会保留元数据。
-
@ajt 好像那个网站刚刚在复制 Stackoverflow !!!问题是逐字逐句
-
@Datadimension word for word o.0 yup copying stackoverflow
标签: php laravel google-api-php-client google-api-client