我正在将一个 zip 文件上传到共享驱动器,但由于我不想在上传之前复制它,所以我试图删除它。
上传正常。但是删除会导致错误:
Uncaught Google\Service\Exception: { “error”: { “code”: 403, “message”: “The user does not have sufficient permissions for this file.“, “errors”: [ { “message”: “The user does not have sufficient permissions for this file.“, “domain”: “global”, “reason”: “insufficientFilePermissions” } ] } }
我是共享盘的管理员,所以我有所有的权限。
这是我的代码:
require '../vendor/autoload.php';
if(session_status() == PHP_SESSION_NONE)
session_start();
$client = new Google_Client();
// credenziali google e scope
$client->setClientId('client id');
$client->setClientSecret('client secret');
$client->setRedirectUri('redirect puri');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->fetchAccessTokenWithAuthCode($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
}
else
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Drive($client);
//HERE WAS THE CODE TO GET THE NAME OF THE FILE
// ----
//check if file exists
$optParams = array(
'pageSize' => 1000,
'supportsAllDrives' => true,
'includeItemsFromAllDrives' => true,
'driveId' => '***', // id of shared drive
'corpora' => 'drive',
'fields' => 'files(id, name)',
'q' => "name='" . $name . "2023.zip' and trashed = false"
);
$results = $service->files->listFiles($optParams);
// delete latest version already on drive
if(count($results->getFiles()) == 1) {
$service->files->delete($results->getFiles()[0]->getId(), array('supportsAllDrives' => true));
}
//upload on drive of the new version
$file = new Google_Service_Drive_DriveFile();
$file->setName($name); // name of the file
$file->setMimeType('application/zip'); // type
$file->setParents(['***']); // id of shared drive
$data = file_get_contents("path"); // path to file (hidden for the question)
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'application/zip',
'uploadType' => 'multipart',
'supportsAllDrives' => true
));
} else {
// name saving
file_put_contents('code.txt', $_GET['name']);
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}