有什么方法可以使用 google drive api v3 for PHP 来删除文件吗?

Is there any way to trash a file using google drive api v3 for PHP?

我正在寻找一种方法来删除文件,而不必使用 PHP 的 API v3 从 google 驱动器中永久丢失它们...我的意思是,我想把它扔掉。

查看 V2 参考,我注意到有一个直接函数可以执行此操作,但在 V3 中它不再存在并且不起作用。

/**
* Move a file to the trash.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param String $fileId ID of the file to trash.
* @return Google_Servie_Drive_DriveFile The updated file. NULL is returned if
*     an API error occurred.
*/
function trashFile($service, $fileId) {
 try {
   return $service->files->trash($fileId);
 } catch (Exception $e) {
   print "An error occurred: " . $e->getMessage();
 }
 return NULL;
} 

所以,有没有办法用 Google 驱动 PHP API V3trash 一个文件 and/or 文件夹而不会永久丢失?

我认为您的目标和现状如下。

  • 您想使用 Drive API v3 和用于 PHP 的 googleapis 将文件和文件夹移动到垃圾箱。
  • 您已经可以使用 Drive API v3 并且您的 $service 可用于将文件移动到垃圾箱。

本例使用Drive API v3的“Files: update”方法。示例脚本如下

示例脚本:

$fileId = '###';  // Please set the file ID and folder ID.

$metadata = new Google_Service_Drive_DriveFile();
$metadata->setTrashed(true);
$res = $service->files->update($fileId, $metadata);

参考: