Repository navigation
Google Drive Upload - Last Chunk Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Failed to parse Content-Range header.' #1227
Description
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
prtk418 commentedon Jan 22, 2018
@bazzhar70 were you able to solve this?
i am also facing same issue:
levieraf commentedon Mar 29, 2018
Hi guys this is what I do to upload files to drive, this works great for me, take what you need and go ahead:
mattwhisenhunt commentedon Apr 23, 2018
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.
kocoten1992 commentedon May 17, 2018
In my case, I was upload a directory 🤕 , something like this:
levieraf commentedon May 17, 2018
@kocoten1992 and how did you fix it? I mean how did you resolve upload the document in the end?
kocoten1992 commentedon May 17, 2018
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.
levieraf commentedon May 17, 2018
@kocoten1992 ohhh nice! well, remember to bring me a coffee xD!
duvanvargas commentedon May 25, 2018
Im having the same issue