Google Drive API - PHP Client Library - setting uploadType to resumable upload

前端 未结 2 1381
灰色年华
灰色年华 2020-12-03 18:25

I am having serious issues with the documentation for the new google drive API client library. It seems this should be an easy one to answer without having to put it on stac

2条回答
  •  星月不相逢
    2020-12-03 19:14

    The following sample will work with the latest version of the Google APIs PHP Client (https://code.google.com/p/google-api-php-client/source/checkout)

    if ($client->getAccessToken()) {
      $filePath = "path/to/foo.txt";
      $chunkSizeBytes = 1 * 1024 * 1024;
    
      $file = new Google_DriveFile();
      $file->setTitle('My document');
      $file->setDescription('A test document');
      $file->setMimeType('text/plain');
    
      $media = new Google_MediaFileUpload('text/plain', null, true, $chunkSizeBytes);
      $media->setFileSize(filesize($filePath));
    
      $result = $service->files->insert($file, array('mediaUpload' => $media));
    
      $status = false;
      $handle = fopen($filePath, "rb");
      while (!$status && !feof($handle)) {
        $chunk = fread($handle, $chunkSizeBytes);
        $uploadStatus = $media->nextChunk($result, $chunk);
      }
    
      fclose($handle);
    }
    

提交回复
热议问题