如何使用 PHP 中的 Google Drive API 将内容添加到文件?

问题描述 投票:0回答:1

我想将一个超过 10 GB 的大文件上传到 Google 云端硬盘。在谷歌的帮助下,我编写了以下代码:

public function upload($pathToFile) {
    $client = $this->client;
    $service = $this->drive;

    $fname = basename($pathToFile);
    $file = new Google_Service_Drive_DriveFile([
        'name' => $fname,
    ]);
    $chunkSizeBytes = 1 * 1024 * 1024 * 200;

    $request = $service->files->create($file);
    $fileId = $request->id;

    $media = new Google_Http_MediaFileUpload(
        $client,
        $request,
        'text/plain',
        null,
        true,
        $chunkSizeBytes
    );
    $media->setFileSize(filesize($pathToFile));

    $status = false;
    $handle = fopen($pathToFile, "rb");
    while (!$status && !feof($handle)) {
        $chunk = fread($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
    }
    return $status != false;
}

我收到此错误:

Fatal error: Uncaught TypeError: Argument 2 passed to Google\Http\MediaFileUpload::__construct() must implement interface Psr\Http\Message\RequestInterface, instance of Google\Service\Drive\DriveFile given, called

我该如何修复它?

php google-drive-api
1个回答
0
投票

v2.12.6 版本应该修复此问题。这是拉取请求,确认https://github.com/googleapis/google-api-php-client/releases/tag/v2.12.6

从以前的版本中,这一行在某些时候被更改并导致了副作用错误。根据他们的存储库。所以OP你的创建是正确的。谷歌有很多方法可以做我说错的事情,而无需深入研究。

        $response = $this->client->execute($this->request, null);
        $response = $this->client->execute($this->request, false);

如何以已确认有效的方式实现这一点的示例如下:

        $gfmeta = new Google_Service_Drive_DriveFile();
        $gfmeta->name = $backupFileName;
        $gfmeta->parents = array($GoogleDriveBackupParentFolderID);
        $backupFilePath = dirname(__FILE__) . "/".$backupFileName;

        // Call the API with the media upload, defer so it doesn't immediately return.
        $GoogleClient->setDefer(TRUE);
        $guploadRequest = $GoogleDriveApp->files->create($gfmeta);

        // Create a media file upload to represent our upload process
        $media = new Google_Http_MediaFileUpload(
            $GoogleClient,
            $guploadRequest,
            mime_content_type($backupFilePath),
            NULL,
            TRUE,
            $GoogleDriveUploadChunkSizeBytes
        );
        $media->setFileSize(filesize($backupFilePath));
        
        // Upload the various chunks. $uploadStreamStatus will be false until complete
        $uploadStreamStatus = FALSE;
        $backupFileHandle = fopen($backupFilePath, "rb");
        while(!$uploadStreamStatus && !feof($backupFileHandle)) {
            $fileChunk = readFileChunk($backupFileHandle, $GoogleDriveUploadChunkSizeBytes);
            $uploadStreamStatus = $media->nextChunk($fileChunk);
        }
        // Take care of the file handle and file creation in Google
        $GoogleClient->setDefer(FALSE);
        fclose($backupFileHandle);

        // The final value of $uploadStreamStatus will be the data from the API for the upload
        $result = FALSE;
        if($uploadStreamStatus !== FALSE) {
                $result = $uploadStreamStatus;
        } else {
        #       throw new Exception("Upload Error!");
        }       

我相信,如果您使用具有类似代码流程结构的版本,它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.