php 如何上传文件到Google驱动器的共享驱动器文件夹?

4c8rllxm  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(210)

实际上这个代码是上传文件到正常的Gdrive,但我需要上传我的文件到共享驱动器,有人能帮助我。所有我需要的是上传我的文件到我的共享驱动器。下面是我目前的代码:-

require __DIR__ . '/vendor/autoload.php';

use Google\Client;
use Google\Service\Drive;
# TODO - PHP client currently chokes on fetching start page token
function uploadBasic()
{
    try {
        $client = new Client();
        putenv('GOOGLE_APPLICATION_CREDENTIALS=./credentials.json');
        $client->useApplicationDefaultCredentials();
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);

        $file = getcwd().'/637fdc0994855.mp4';
        $filename = basename($file);
        $mimeType = mime_content_type($file);

        $fileMetadata = new Drive\DriveFile(array(
            'name' => $filename,
            'parents' => ['1VBi8C04HBonM6L4CfL-jHWZ0QoQRyrCL']
        ));
        $content = file_get_contents('637fdc0994855.mp4');
        $file = $driveService->files->create($fileMetadata, array(
            'data' => $content,
            'mimeType' => $mimeType,
            'uploadType' => 'multipart',
            'fields' => 'id'
        ));
        printf("File ID: %s\n", $file->id);
        return $file->id;
    } catch (Exception $e) {
        echo "Error Message: " . $e;
    }
}
uploadBasic();
qxsslcnc

qxsslcnc1#

为了将文件上传到共享驱动器,请进行如下修改。

发件人:

$file = $driveService->files->create($fileMetadata, array(
    'data' => $content,
    'mimeType' => $mimeType,
    'uploadType' => 'multipart',
    'fields' => 'id'
));

收件人:

$file = $driveService->files->create($fileMetadata, array(
    'data' => $content,
    'mimeType' => $mimeType,
    'uploadType' => 'multipart',
    'fields' => 'id',
    'supportsAllDrives' => true // <--- Added
));

注:

  • 在这种情况下,您的客户端没有权限写入共享驱动器,因此会出现错误。请注意这一点。

参考:

相关问题