gpt4 book ai didi

php - 文件移动 Google Drive API v3 PHP

转载 作者:行者123 更新时间:2023-12-04 14:50:18 27 4
gpt4 key购买 nike

使用 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)中使用的对象的字段(即使我只是更新文件 parent - 这是可写的)。
另见 https://issuetracker.google.com/issues/199921300

到目前为止答案的问题:
但感谢回应
$fromFile = $this->service->files->get($fileID, ['fields' => 'parents, id']);
返回所有 ~75 个属性,其中很多是不可写的。
而不是 PHPStorm 调试中预期的 2(注意中断位于紧跟 GET 请求之后的语句中,因此此时无关紧要
enter image description here
使用
unset($fromFile->shared);
仍然留下其他可写属性
实际上该文件实际上并未共享
enter image description here

更新我的编码
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;
}
这证明有点成功,但这是原始元
enter image description here
上面的代码确实移动了它,看似正确的元数据,创建时间和 EXIF 数据现在完好无损
enter image description here

最佳答案

问题是您使用的是 file.update使用 HTTP PATCH方法。通过使用 PATCH,它会尝试更新您发送的文件对象中的所有属性。
你做了一个 file.get 并且你包含了文件 *

 $fromFile = $this->service->files->get($fileID, ['fields' => '*']);
通过包含 'fields' => '*'您告诉 API 将文件具有的所有属性返回给您。 A file resource有很多属性,它们并不是都可写的。
通过向 file.update 方法发送所有字段,您告诉它您想要更新所有内容,包括一些您不允许更新的属性。 api 响应 fieldNotWritable解决方案是执行以下操作
$fromFile = $this->service->files->get($fileID, ['fields' => 'parents, id']);
这将导致该方法仅返回您实际需要的两个参数。 id 和parents 参数。 (TBH 你可能只需要 parent 部分,我需要测试一下。)
{
"id": "1x8-vD-XiA5Spf3qp8x2wltablGF22Lpwup8VtxNY",
"parents": ["0B1bbSFgVLpoXcVDRFRF8tTkU"
]
}
然后应该允许您更新父级并移动您的文件。

关于php - 文件移动 Google Drive API v3 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69182556/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com