flysystem-google-drive-ext icon indicating copy to clipboard operation
flysystem-google-drive-ext copied to clipboard

GoogleDriveAdapter => UnableToReadFile error

Open yagrasdemonde opened this issue 1 year ago • 1 comments

Context

Hello, I'm running Laravel 9.22.1 with PHP 8.1 with those packages :

version
masbug/flysystem-google-drive-ext v2.2.2
spatie/laravel-backup v8.1.3

I created my credentials to access to Google Drive and I setup accordingly your readme my config files : .env, filesystems.php

My configuration works, if I check

dd(Storage::disk('google'));

I can see properties like driver, adapter

I'm trying to backup database with backup:run --only-db but I have this error in terminal :

Copying zip failed because: Could not connect to disk google because: League\Flysystem\UnableToReadFile: Unable to read file from location: projectv9_local_agency. File not found in /Volumes/Data/WEB/www-local/lab/projectv9/vendor/league/flysystem/src/UnableToReadFile.php:24

Stack trace:

#0 /Volumes/Data/WEB/www-local/lab/projectv9/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(1904): League\Flysystem\UnableToReadFile::fromLocation('projectv9_lo...', 'File not found')

#1 /Volumes/Data/WEB/www-local/lab/projectv9/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(2002): Masbug\Flysystem\GoogleDriveAdapter->makeFullVirtualPath('projectv9_lo...', false)

#2 /Volumes/Data/WEB/www-local/lab/projectv9/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(808): Masbug\Flysystem\GoogleDriveAdapter->toVirtualPath('projectv9_lo...')

#3 /Volumes/Data/WEB/www-local/lab/projectv9/vendor/league/flysystem/src/DirectoryListing.php(33): Masbug\Flysystem\GoogleDriveAdapter->listContents('projectv9_lo...', false)

So how do I have to make it work ?

Thank you very much

yagrasdemonde avatar Aug 05 '22 09:08 yagrasdemonde

https://github.com/erikn69/laravel-google-drive-ext-demo Look at folder/file permissions

erikn69 avatar Aug 05 '22 13:08 erikn69

Please explain little more. What kind of file permissions? drive? backup? framework? storage?

Pushkraj19 avatar Feb 20 '23 09:02 Pushkraj19

Found the solution

You need to change your backup.php config app name to your google drive folder name

`<?php

return [

'backup' => [

    /*
     * The name of this application. You can use this name to monitor
     * the backups.
     * changed it to drive folder name
     */
    'name' => env('GOOGLE_DRIVE_FOLDER_NAME', 'default'),
    `

And remove GOOGLE_DRIVE_FOLDER from env

Pushkraj19 avatar Feb 20 '23 09:02 Pushkraj19

Can you clarify the fix please...

I still have a problem if I specify a folder name in my .env file (example below)

The only way it works is if I leave "GOOGLE_DRIVE_FOLDER=" blank in my .env - the files are then backed up to my root Google Drive directory.

.env

FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=xxxx
GOOGLE_DRIVE_CLIENT_SECRET=xxxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxxx
GOOGLE_DRIVE_FOLDER="BackUps"

config/backup.php 'name' => env('GOOGLE_DRIVE_FOLDER', 'laravel-backup'),

config/filesystems.php

        'google' => [
            'driver' => 'google',
            'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
            'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
            'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
            'folder' => env('GOOGLE_DRIVE_FOLDER'), // without folder is root of drive or team drive
        ],

GoogleDriveServiceProvider:

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

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

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

                $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\GoogleDriveAdapter($service, $config['folder'] ?? '/', $options);
                $driver = new \League\Flysystem\Filesystem($adapter);

                return new \Illuminate\Filesystem\FilesystemAdapter($driver, $adapter);
            });
        } catch(\Exception $e) {
            // your exception handling logic
        }
        // ...
    }


sutcliffe121 avatar Jan 28 '24 17:01 sutcliffe121