使用Drive SDK将大的zip文件上传到Google Drive

时间:2018-08-16 12:48:07

标签: php google-drive-api drive

您好,我正在尝试上传一个大型zip文件以进行驱动。我尝试了new Google_Http_MediaFileUpload(),但它给了我这个错误。

  

PHP致命错误:未捕获Google_Exception:无法启动断点续传(HTTP 200)

我不知道为什么会这样,所以任何人都可以告诉我这可能是问题还是为什么我的脚本抛出此异常。

这是我的代码,

$AT = Helper::get_Settings('google_token');

    $client = new Google_Client();
    $client->setClientId('Client_ID');
    $client->setClientSecret('Client_Secret');
    $client->setScopes(array('https://www.googleapis.com/auth/drive'));

    $client->setAccessToken($AT);

    // Making a new drive service
    $service = new Google_Service_Drive($client);

    // Making directory structure into google drive
    $dirs = explode('/', $dir);
    $parentid = '';
    foreach ($dirs as $new_dir) {
        $dirSt = new Google_Service_Drive_DriveFile();
        $dirSt->setName($new_dir);
        $dirSt->setMimeType('application/vnd.google-apps.folder');
        if ($parentid != '') {
            $dirSt->setParents(array($parentid));
        }
        $new_file = $service->files->create($dirSt, array(
            'fields' => 'id'));

        $parentid = $new_file->id;
    }

    // Uploading Backed up files
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $file_path = wp_upload_dir()['basedir'] . '/' . $fileName . '.zip';
    $fileSize = filesize($file_path);
    $chunkSizeBytes = 1 * 1024 * 1024;
    $mime_type = 'application/zip';

    $fileMetadata = new Google_Service_Drive_DriveFile(array(
        'name' => $fileName,
        'parents' => array($parentid)
    ));

    $client->setDefer(true);

    $request = $service->files->create($fileMetadata);

    // Create a media file upload to represent our upload process.
    $media = new Google_Http_MediaFileUpload($client, $request, $mime_type, $chunkSizeBytes);
    $media->setFileSize($fileSize);

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

    $result = false;
    if ($status != false) {
        $result = $status;
    }

    fclose($handle);
    $client->setDefer(false);

现在为什么会引发此异常,我被困在这里。

0 个答案:

没有答案