Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working within Shared Drives #1

Closed
rjshreve opened this issue Mar 9, 2022 · 15 comments
Closed

Working within Shared Drives #1

rjshreve opened this issue Mar 9, 2022 · 15 comments

Comments

@rjshreve
Copy link

rjshreve commented Mar 9, 2022

Hello - I'm thinking this is less of an issue and more of just me not understanding but I'm trying to work within Shared Drives and not having any luck. The structure I'm going for is:

Shared Drive

  • Sub Folder 1
    • File 1
  • Sub Folder 2
    • File 1
    • File 2

I've been able to use Storage::disk('google')->makeDirectory('Sub Folder 1') and that works fine. The problem comes when I try to use Storage::disk('google')->put('Sub Folder 1/File 1.txt', 'Hello World'); - In this case it ends up creating a second "Sub Folder 1" and then I get the error in Laravel saying "Unable to read file from location: [shared drive id]/Sub Folder 1. File not found"

How do I go about utilizing the standard Laravel Storage Facade inside of folders within a shared drive?

Thanks in advance!

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

Try to never use makeDirectory, just use Storage::disk('google')->put('Sub Folder 1/File 1.txt', 'Hello World');, the package creates all the folders tree

@rjshreve
Copy link
Author

rjshreve commented Mar 9, 2022

@erikn69 Thanks for the response. I have done that too but it only creates the directory then still gives me the error "Unable to read file from location: [shared drive id]/Sub Folder 1. File not found". It seems like it won't do anything INSIDE of a folder that is inside of a Shared Drive. It will only create new files in the root of the Shared Drive itself.

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

Weird, i'm using team drives without problems

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

Did you config team drives??

@rjshreve
Copy link
Author

rjshreve commented Mar 9, 2022

I have the shared drive ID store to "GOOGLE_DRIVE_TEAM_DRIVE_ID" in my env file. I have my "GOOGLE_DRIVE_FOLDER" left set as "GOOGLE_DRIVE_FOLDER=". To be clear, this used Google Shared Drives since "Team Drives" is not longer a thing.

@rjshreve
Copy link
Author

rjshreve commented Mar 9, 2022

I did notice that if I specify something like "GOOGLE_DRIVE_FOLDER='files'" in my env file then it works but it's creating it using the following structure:

  • Shared Drive
    -- files (created based on name of GOOGLE_DRIVE_FOLDER env variable)
    --- [shared drive id] (created based on name of GOOGLE_DRIVE_TEAM_DRIVE_ID env variable)
    ---- Sub Folder 1
    ----- file 1.txt with content

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

maybe you have to remove GOOGLE_DRIVE_FOLDER if you want the root

@rjshreve
Copy link
Author

rjshreve commented Mar 9, 2022

Nope, still doesn't seem to do it, unfortunately

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

do this on filesystem.php, comment

 //'folder' => env('GOOGLE_DRIVE_FOLDER ')

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

To be clear, this used Google Shared Drives since "Team Drives" is not longer a thing.

I don't understand that

here is my config

    'google' => [
            'driver' => 'google',
            'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
            'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
            'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
            'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
        ],

And my provider

$adapter = new \Masbug\Flysystem\GoogleDriveAdapter($service, $config['folder'] ?? '/', $options);

On default folder is '/'

@rjshreve
Copy link
Author

rjshreve commented Mar 9, 2022

To be clear, this used Google Shared Drives since "Team Drives" is not longer a thing.

I don't understand that

Google doesn't speak much too it but it seems like more of a "name change" vs actual differences in how it functions I think but notably, the API docs call out that supportsTeamDrives has been replaced by supportsAllDrives: https://developers.google.com/drive/api/v3/reference/files/create?hl=en_US

https://documentation.its.umich.edu/node/669#:~:text=Google%20shared%20drives%20(formerly%20known,shared%20drives%20and%20My%20Drive.

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

On default folder must be '/',

Google Shared Drives / Team Drives, are both the same?

@rjshreve
Copy link
Author

rjshreve commented Mar 9, 2022

my config:

'google' => [
      'driver' => 'google',
      'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
      'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
      'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
      'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
  ]

My provider's boot:

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

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

                $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
        }

@erikn69
Copy link
Owner

erikn69 commented Mar 9, 2022

I really don't know, it is working for me, sorry

@erikn69
Copy link
Owner

erikn69 commented Mar 16, 2022

Hi, i have time now, and i did test this
Maybe you are using shared folder intead of shared drive, it works like a regular folder, so your config must be

'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'),
  ]

.env

GOOGLE_DRIVE_FOLDER=NAME_OF_YOUR_SHARED_FOLDER

Look at this app, it works on a shared folder

Route::get('put-in-dir', function() {
Storage::cloud()->put('Test Dir/test.txt', 'Hello World');
return 'File was created in the sub directory in Google Drive';
});

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants