I’m working with Google Drive API using PHP Client Library and need help with resumable uploads. I can already upload files in chunks but I want to track upload progress and handle interruptions properly.
function processFileUpload($driveService, $apiClient, $targetFile, $folderId) {
$driveFile = new Google_Service_Drive_DriveFile();
$driveFile->name = $targetFile['filename'];
$pieceSize = 2 * 1024 * 1024;
if ($folderId != null) {
$folder = new Google_Service_Drive_ParentReference();
$folder->setId($folderId);
$driveFile->setParents(array($folder));
}
$apiClient->setDefer(true);
$uploadRequest = $driveService->files->insert($driveFile);
$mediaUploader = new Google_Http_MediaFileUpload(
$apiClient,
$uploadRequest,
$targetFile['mimetype'],
null,
true,
$pieceSize
);
$mediaUploader->setFileSize(filesize($targetFile['path']));
$uploadComplete = false;
$fileHandle = fopen($targetFile['path'], "rb");
while (!$uploadComplete && !feof($fileHandle)) {
set_time_limit(180);
$dataPiece = fread($fileHandle, $pieceSize);
$uploadComplete = $mediaUploader->nextChunk($dataPiece);
}
fclose($fileHandle);
$apiClient->setDefer(false);
return $uploadComplete;
}
The problem is I need to check upload status and resume failed uploads. How can I capture the session URI and make custom requests to check progress? Do I need a separate script for handling these status checks?