Skip to content

Google Drive Upload - Last Chunk Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Failed to parse Content-Range header.' #1227

Closed

Description

@bazzhar70

I am having problem with resumable upload. I am uploading a video to my drive. The last chunk size for my upload always produce "Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Failed to parse Content-Range header."

Almost 99% of my upload is sent but every time the upload is about to finish, the error always shows up. I have tried formats like .mp4 and .mkv file with different sizes; 800MB,1.5Gig,3Gig,4Gig but same thing happens.

I examine the Response Header and noticed the server returns error code 400. The "[X-GUploader-UploadID]" header returns 2 element with the same value which I think is part of the resume URI.
Also, the "Range" and somewhat "md5" like content is missing for the last chunk upload.

Please enlighten me.

Here is the upload code

$resumeUri = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=AEnB2Uqcn7Z4JGrdwP8PaivzBjLzlix5pzGZFLJaaRqaYD6vnWAufOTDw5Ptch8CymX3s_lgXH3aCWOdTKzeyFwstpjhZiDiO_c6HxTQA4EybFkQvv9tggw";

$file = new Google_Service_Drive_DriveFile();
$file->title = "Big File";
$file->setName($fname);
$file->setParents(array($parentFolder));


$chunkSizeBytes = 256*1024*20;

// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
  $client,
  $request,
  $mtype,
  null,
  true,
  $chunkSizeBytes
);

$status = false;
$handle = fopen($filePath, "rb");

$chunckTotalSent = 0;
$fileSize = getFileSizePerl($filePath);
$media->setFileSize($fileSize);

$resChunkSize = $media->resume($resumeUri);
while (!$status && !feof($handle)) {
  if ($client->isAccessTokenExpiredInNSeconds(120)) {
      $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
      print "/////////////////////////////////////////////////////////\n";
      print "[   REFRESH TOKEN USED TO FETCH ACCESS TOKEN  IN       ]\n";
      print "/////////////////////////////////////////////////////////\n";
      $rt = TOKEN_REFRESH;
      $client->fetchAccessTokenWithRefreshToken($rt);      
      // $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());      
      file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
      $accessToken = $client->getAccessToken();
      $client->setAccessToken($accessToken);
  }//end else

  print "File name : ".$fname." - ".$mtype."\n";
  print "Before fread\n";
  $chunk = fread($handle, $chunkSizeBytes);
  print "Before next_chunk\n";
  $status = $media->nextChunk($chunk);
  print "After next_chunk\n";

  $chunckTotalSent += $chunkSizeBytes;
  $progressSize = $media->getProgress();
  $percentComplete = ((100/$fileSize)*$progressSize);
  $fileContent =  
  " %Completed Upload : ".$percentComplete." \n".
  " File size : ".$fileSize." \n".
  " File Path : ".$filePath." \n".
  " MIME Type : ".$mtype." \n".
  " File in Bytes Sent : $chunckTotalSent \n".
  " Progress ".$progressSize."\n".
  " HTTPResultCode ".$media->getHttpResultCode()."\n".
  " File Size Sent ".$chunckTotalSent."\n".
  " Resume URI ".$media->getResumeUri()."\n".
  " ============================================================\n\n";
  //file_put_contents($assocFilename, json_encode($fileContent));
  print $fileContent;
 }

// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if($status != false) {
  $result = $status;
}

fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);

PS.
$client->isAccessTokenExpiredInNSeconds($seconds) is the custom method I made. No other custom
methods aside from that.

Activity

  1. prtk418 commented on Jan 22, 2018

    @prtk418

    @bazzhar70 were you able to solve this?

    i am also facing same issue:

    PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Failed to parse Content-Range header.' in vendor/google/apiclient/src/Google/Http/REST.php:118
    
    Stack trace:
    #0 vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), NULL)
    #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), NULL)
    #2 vendor/google/apiclient/src/Google/Task/Runner.php(176): call_user_func_array(Array, Array)
    #3 vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run()
    #4 vendor/google/apiclient/src/Google/Client.php(788): Google_Http_R in /var/www/html/website-redesign/application/libraries/hla_app_log/vendor/google/apiclient/src/Google/Http/REST.php on line 118
  2. levieraf commented on Mar 29, 2018

    @levieraf

    Hi guys this is what I do to upload files to drive, this works great for me, take what you need and go ahead:

    $file = new \Google_Service_Drive_DriveFile($options);
    $file->title = $data['file_name'];
    
    $chunkSizeBytes = 100 * 1024 * 1024; // 100 MB
    
    // Call the API with the media upload, defer so it doesn't immediately return.
    $client->setDefer(true);
    
    $request = $service->files->create($file, [ 'uploadType' => 'resumable', 'fields' => 'id' ]);
    
    // Create a media file upload to represent our upload process.
    $media = new \Google_Http_MediaFileUpload(
        $client,
        $request,
        mime_content_type($data['filepath']),
        null,
        true,
        $chunkSizeBytes
    );
    
    $media->setFileSize(filesize($data['filepath']));
    
    $media->setChunkSize($chunkSizeBytes);
    
    $status = false;
    
    $handle = fopen($data['filepath'], "rb");
    
    while(!$status && !feof($handle)){
        $chunk = fread($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
    }
    
    $result = false;
    if($status != false){
        $result = $status;
    }
    
    fclose($handle);
    
    // Reset to the client to execute requests immediately in the future.
    $client->setDefer(false);
    
    print is_object($result) && isset($result->id) ? $result->id : false;
    
    
    
  3. mattwhisenhunt commented on Apr 23, 2018

    @mattwhisenhunt
    Contributor

    It looks like a member of our great community answered your questions. Thanks Community! Please add a comment if you have any more questions and we can reopen this issue.

  4. kocoten1992 commented on May 17, 2018

    @kocoten1992

    In my case, I was upload a directory 🤕 , something like this:

    $gdrive->resumableUpload('/home/myuser/Downloads');
    
  5. levieraf commented on May 17, 2018

    @levieraf

    @kocoten1992 and how did you fix it? I mean how did you resolve upload the document in the end?

  6. kocoten1992 commented on May 17, 2018

    @kocoten1992

    it was thank to you @levieraf , my code is similar to your, and you said it work, so I take a step back and realize my mistake.
    The error message was no helping though.

  7. levieraf commented on May 17, 2018

    @levieraf

    @kocoten1992 ohhh nice! well, remember to bring me a coffee xD!

  8. duvanvargas commented on May 25, 2018

    @duvanvargas

    Im having the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions