【发布时间】:2020-08-19 15:13:25
【问题描述】:
我一直在尝试设置 Google Drive PHP API 以使用以下代码将简单文件上传到共享驱动器。
<?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 中的某些参数已被弃用,但我很确定我使用的所有参数都没有被弃用。我不确定我做错了什么,所以任何额外的指导将不胜感激,谢谢!
【问题讨论】:
-
This 可能是一个解决方案,因为它看起来与共享驱动器类似的问题?
-
还有一种通过API设置权限的方法,你可能需要单独调用,传递初始上传方法返回的文件ID。
标签: php google-drive-api google-api-php-client google-workspace service-accounts