Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Arturexe's avatar

Uploading files to Google Drive slows down website

I'm building my first gallery site, which includes uploading large amounts of images to an external storage (in my case Google Drive). For that I'm using this driver:

https://github.com/masbug/flysystem-google-drive-ext

My loop looks like this (currently placed in a Controller):

    foreach ($request->chunk as $image) {

        $mime = mime_content_type($image);
        $ext = explode('/', $mime)[1];
        $src = str_replace('data:'.$mime.';base64,', '', $image);

        $imageName = uniqid();
        $imageId = $imageName.'.'.$ext;

        Storage::disk('google')->put($imageId, base64_decode($src));
        $imageUrl = Storage::disk('google')->url($imageId);

    }

This stores each image in google drive and gets their urls, for storing in a local DB. Problem is that during this process, my web app gets "stuck", so that it becomes impossible to even load the site. I don't know if this is tied to my local bandwidth (120Mbs). I've not looked into Queues and Jobs yet but am planning to, but I'm not sure if this problem falls into the same categorie. I've read that Laravel offers some drivers for queues (redis, amazon etc.) but not google drive.

Here's my GoogleDriveServiceProvider.php:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Storage;
use Google\Client;
use Google\Service\Drive;
use Masbug\Flysystem\GoogleDriveAdapter;
use League\Flysystem\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;


class GoogleDriveServiceProvider extends ServiceProvider
{

    public function register()
    {
        //
    }

    public function boot()
    {
        try {
            Storage::extend('google', function($app, $config) {
                $options = [];

                if (!empty($config['teamDriveId'] ?? null)) {
                    $options['teamDriveId'] = $config['teamDriveId'];
                }

                $client = new Client();
                $client->setClientId($config['clientId']);
                $client->setClientSecret($config['clientSecret']);
                $client->refreshToken($config['refreshToken']);

                $service = new Drive($client);
                
                $adapter = new GoogleDriveAdapter($service, $config['folder'] ?? '/', $options);
                $driver = new Filesystem($adapter);

                return new FilesystemAdapter($driver, $adapter);
            });
        } catch(\Exception $e) {
            return $e->getMessage();
        }
    }
}

How would you approach this problem?

Appreciate your help.

0 likes
3 replies
Tray2's avatar

I would probably, store the images locally on the web server first, then schedule a job that takes care of those images in the background, I would probably rate limit that one so that it doesn't use up all the bandwidth. It might be a good thing to look into image sizes as well.

Snapey's avatar

in a similar situation, I upload locally to a pending folder and then later via an overnight job, copy the files to a CDN

Because I did not want to store the full image path in the database, I added a column which stores which storage disk the file is on, so that when serving the image through blade I can construct the right url depending on if the file has been moved yet or not

1 like

Please or to participate in this conversation.