Optimizing and Accelerating File Upload Speed to Google Drive in PHP – Laravel

I’m using Google Drive to upload images, videos, etc. However, I find its performance quite slow, even though I’ve implemented chunking. Is there any optimization method to boost the upload speed? Or are there any CDNs similar to Google Drive but with faster speeds? Please help me address this issue. Thank you :<

 public static function uploadFile($file)
{
    $client = self::getClient();
    $driveService = new Google_Service_Drive($client);

    // Check if 'social' folder exists
    $socialFolderId = self::checkFolderExists($driveService, 'social');

    // If 'social' folder doesn't exist, create it
    if (!$socialFolderId) {
        $folderMetadata = new Google_Service_Drive_DriveFile([
            'name' => 'social',
            'mimeType' => 'application/vnd.google-apps.folder',
        ]);
        $createdFolder = $driveService->files->create($folderMetadata, [
            'fields' => 'id',
        ]);
        $socialFolderId = $createdFolder->id;
    }

    // Create a folder name based on the date
    $dateFolderName = date('d-m-Y');
    $dateFolderExists = self::checkFolderExists($driveService, $dateFolderName);

    // If the date folder doesn't exist, create it in the 'social' folder
    if (!$dateFolderExists) {
        $folderMetadata = new Google_Service_Drive_DriveFile([
            'name' => $dateFolderName,
            'parents' => [$socialFolderId],
            'mimeType' => 'application/vnd.google-apps.folder',
        ]);
        $createdFolder = $driveService->files->create($folderMetadata, [
            'fields' => 'id',
        ]);
        $folderId = $createdFolder->id;
    } else {
        // If the date folder exists, use its id
        $folderId = $dateFolderExists;
    }

    // Generate a new file name
    $newFileName = bin2hex(random_bytes(16)) . '.' . $file->getClientOriginalExtension();

    // Define allowed extensions
    $allowedExtensions = ['png', 'jpg', 'exe', 'vtt', 'mp4', 'txt', 'zip', 'rar', 'docx', 'pdf', 'msi'];
    $fileExtension = $file->getClientOriginalExtension();

    if (in_array($fileExtension, $allowedExtensions)) {
        // Define the chunk size
        $chunkSizeBytes = 10 * 1024 * 1024; // 1MB

        // Call the API with the media upload, defer so it doesn't immediately return.
        $client->setDefer(true);

        // File metadata
        $fileMetadata = new Google_Service_Drive_DriveFile([
            'name' => $newFileName,
            'parents' => [$folderId],
        ]);

        // Create a request
        $request = $driveService->files->create($fileMetadata);

        // Create a media file upload to represent our upload process.
        $media = new Google_Http_MediaFileUpload(
            $client,
            $request,
            'application/octet-stream',
            null,
            true,
            $chunkSizeBytes
        );

        // Set file size
        $media->setFileSize(filesize($file->getRealPath()));

        // Upload the various chunks.
        // The status will be false until the process is complete.
        $status = false;

        // Open file handle
        $handle = fopen($file->getRealPath(), "rb");
        $totalFileSize = filesize($file->getRealPath());
        $uploadedSize = 0;

        while (!$status && !feof($handle)) {
            // Read a chunk of file
            $chunk = fread($handle, $chunkSizeBytes);

            // Upload the chunk
            $status = $media->nextChunk($chunk);
            $uploadedSize += strlen($chunk);

            // Calculate the progress based on the uploaded size
            $progress = ($uploadedSize / $totalFileSize) * 100;

            // Send an event with the current progress
            event(new FileUploadProgressEvent($progress));
        }

        // Ensure the final progress is sent, even if it doesn't reach 100%
        if ($progress < 100) {
            event(new FileUploadProgressEvent(100));
        }

        // Close file handle
        fclose($handle);

        // Reset to the client to execute requests immediately in the future.
        $client->setDefer(false);
        // Create permissions for the file
        $permissionMetadata = new Google_Service_Drive_Permission([
            'type' => 'anyone',
            'role' => 'reader',
        ]);

        // Add permissions to the file
        $driveService->permissions->create($status->id, $permissionMetadata);

        // Get file metadata
        $uploadedFile = $driveService->files->get($status->id, ['fields' => 'webViewLink']);
        $downloadLink = "https://drive.google.com/uc?export=download&id=" . $status->id;
        // return ['downloadLink' => $downloadLink, 'webViewLink' => $uploadedFile->webViewLink, 'id' => $status->id];
        return response()->json([
            'success' => true,
            'message' => 'File uploaded successfully!',
            'filename' => $newFileName,
            'filepath' => $downloadLink,
            'fileId' => $status->id
        ]);

    } else {
        return false;
    }
}