我一直在尝试将设置为使用以下代码将一个简单文件上传到共享驱动器。
<?php
require_once '../../../php/Services/JSON.php';
require '../vendor/autoload.php';
require '../helper.php';
$chunkSizeBytes = 1048576;
$client = new Google_Client();
$client->setAuthConfig(__DIR__.'/SERVICE-ACCOUNT-CREDENTIALS.json');
$client->setApplicationName('Uploader');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setDefer(true);
$file = 'testUpload.txt';
$service = new Google_Service_Drive($client);
$params = [
'fields' => 'id',
'supportsAllDrives' => true
];
$req = $service->files->create(new Google_Service_Drive_DriveFile([
'name' => $file,
'teamDriveId' => 'DRIVE ID',
'parents' => '1Ik-tFv8UaOmlnZ3ojgPPba0o3hauh_63',
'mimeType' => Helper::get_mime_type($file)
]), $params);
$media = new Google_Http_MediaFileUpload($client, $req, Helper::get_mime_type($file), null, true, $chunkSizeBytes);
$media->setFileSize(filesize($file));
$status = false;
$fileHandler = fopen($file, 'rb');
while(!$status and !feof($fileHandler)) {
$chunk = fread($fileHandler, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($fileHandler);
$client->setDefer(false);
echo "https://drive.google.com/open?id=".$status['id']."\n";运行此代码后,它将给我一个指向文件的链接,但当我访问该链接时,它说我需要访问该文件的权限。当我打开共享驱动器上的文件夹时,文件就无处可见,因此正在上传,但据我所知,没有上传到正确的位置。我想确保这个文件被上传到共享驱动器和指定的文件夹中,但是到目前为止我还没有这样做。我知道有些参数已经从API中被废弃了,但是我非常肯定我使用的所有参数都不是推荐的。我不知道我做错了什么,所以任何额外的指导都将不胜感激,谢谢!
发布于 2020-08-19 07:11:45
塞维耶的账户不是你。服务帐户将文件上传到那里,因为它拥有该文件。如果您想访问它,可以使用服务帐户,通过permissions.create授予您访问它的权限。
$optParams = array(
'emailMessage' => '[YourValue]',
'sendNotificationEmail' => '[YourValue]',
'supportsTeamDrives' => '[YourValue]',
'transferOwnership' => '[YourValue]',
'fields' => '*'
);
return $service->permissions->CreatePermissions($fileId, $optParams);还记得将supportsAllDrives参数添加到初始文件上传中。
https://stackoverflow.com/questions/63480255
复制相似问题