How To Setup Laravel Backup On Google Drive?

· · 1109 views · Solved

Hello Sir, I am from Ethiopia. Your video on How To Setup Laravel Backup On Google Drive is fantastic! It helped me a lot, but I have some issues.

My league/flysystem is version 3.28.0, so the package composer require nao-pon/flysystem-google-drive:~1.1 is not compatible.

Instead, I used the package: composer require masbug/flysystem-google-drive-ext.

In my code:

public function boot()
{
    \Storage::extend('google', function ($app, $config) {
        $client = new \Google_Client();
        $client->setClientId($config['clientId']);
        $client->setClientSecret($config['clientSecret']);
        $client->refreshToken($config['refreshToken']);
        $service = new \Google_Service_Drive($client);
        $adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);

        return new \League\Flysystem\Filesystem($adapter);
    });
}

I encountered errors when importing Google_Service_Drive and GoogleDriveAdapter. I imported them like this:

use Google_Client;
use Google_Service_Drive;
use GoogleDriveAdapter;

However, I got the following errors:

  • Undefined type 'Google_Service_Drive'
  • Undefined type 'GoogleDriveAdapter'

How can I solve this issue? I also created a GoogleDriveServiceProvider.

0
1 Answer

It looks like you're encountering issues due to missing imports or incorrect namespaces for the Google_Service_Drive and GoogleDriveAdapter classes. Here's how you can resolve this:

1. Correct Imports for Google_Service_Drive

Make sure that the Google_Service_Drive is properly imported from the Google API PHP Client library.

If you haven't already installed it, you need to include the Google Client library in your project:

composer require google/apiclient

Then, use the correct namespace:

use Google_Client;
use Google_Service_Drive;

2. Correct Import for GoogleDriveAdapter

For the GoogleDriveAdapter, ensure that you're using the correct namespace from the masbug/flysystem-google-drive-ext package.

Instead of:

use GoogleDriveAdapter;

You should import it like this:

use Masbug\Flysystem\GoogleDrive\GoogleDriveAdapter;

3. Full Example Code

After fixing the imports, your boot() method should look like this:

public function boot()
{
    \Storage::extend('google', function ($app, $config) {
        $client = new \Google_Client();
        $client->setClientId($config['clientId']);
        $client->setClientSecret($config['clientSecret']);
        $client->refreshToken($config['refreshToken']);
        $service = new \Google_Service_Drive($client);
        $adapter = new \Masbug\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);

        return new \League\Flysystem\Filesystem($adapter);
    });
}

4. Configuring the Service Provider

If you're still facing issues, double-check the configuration of your GoogleDriveServiceProvider. Ensure that the service provider is properly registered in config/app.php under the providers array:

'providers' => [
    // Other Service Providers

    App\Providers\GoogleDriveServiceProvider::class,
],

5. Folder ID Check

Make sure that the folderId in the $config['folderId'] is a valid Google Drive folder ID where you're planning to store the backups.

By following these steps, you should be able to resolve the issues you're encountering.

2
Best answer
Chimdesa Asebe
Chimdesa Asebe ·

I appreciate your quick response. Thank you. However, it's still not working for me. Here is my full code:

composer require masbug/flysystem-google-drive-ext
use Google_Client;
use Google_Service_Drive;
use League\Flysystem\Filesystem;
use Illuminate\Support\Facades\Storage;
use Masbug\Flysystem\GoogleDrive\GoogleDriveAdapter;

public function boot()
{
    Storage::extend('google', function ($app, $config) {
        $client = new Google_Client();
        $client->setClientId($config['clientId']);
        $client->setClientSecret($config['clientSecret']);
        $client->refreshToken($config['refreshToken']);
        $service = new Google_Service_Drive($client);
        $adapter = new GoogleDriveAdapter($service, $config['folderId']);

        return new Filesystem($adapter);
    });
}
  1. Error on Google_Service_Drive:

    $service = new Google_Service_Drive($client);
    
  2. Error on GoogleDriveAdapter:

    $adapter = new GoogleDriveAdapter($service, $config['folderId']);
    

I have already registered my GoogleDriveServiceProvider, but I still face these errors.

Harish Kumar
Harish Kumar ·

Here’s a concise solution for your issue:

1. Ensure Dependencies Are Installed

Install the Google API client if you haven’t already:

composer require google/apiclient:^2.0

2. Check Your Imports

Ensure you’re using the correct namespaces:

use Google_Client;
use Google_Service_Drive;
use League\Flysystem\Filesystem;
use Masbug\Flysystem\GoogleDrive\GoogleDriveAdapter;

3. Clear Composer Autoload

If errors persist, clear the autoload cache:

composer dump-autoload

4. Reinstall the Google Drive Adapter Package

Check if the package is correctly installed. If not, reinstall it:

composer remove masbug/flysystem-google-drive-ext
composer require masbug/flysystem-google-drive-ext

5. Check Configuration

Ensure the Google API credentials (clientId, clientSecret, refreshToken, folderId) are correct and accessible.

By following these steps, your issue should be resolved!

Please login or create new account to participate in this conversation.