04
AprGoogle uses unique IDs for each folder and file. This makes it difficult to integrate with other storage services which use normal paths. This Flysystem adapter works around that problem by seamlessly translating paths from “display paths” to “virtual paths”, and vice versa.
For example, virtual path /Xa3X9GlR6EmbnY1RLVTk5VUtOVkk/0B3X9GlR6EmbnY1RLVTk5VUtOVkk
becomes /My Nice Dir/myFile.ext
and all ID handling is hidden.
For Flysystem V2/V3 or Laravel >= 9.x.x
composer require masbug/flysystem-google-drive-ext
For Flysystem V1 or Laravel <= 8.x.x
use 1.x.x
version of the package
composer require masbug/flysystem-google-drive-ext:"^1.0.0"
Please follow Google Docs to obtain your client ID, client secret & refresh token
.
In addition, you can also check this easy-to-follow tutorial by @ivanvermeyen
Update .env file with google keys
Add the keys you created to your .env file and set google as your default cloud storage. You can copy the .env.example file and fill in the blanks.
FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER=
#GOOGLE_DRIVE_TEAM_DRIVE_ID=xxx
# you can use more accounts, only add more configs
#SECOND_GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
#SECOND_GOOGLE_DRIVE_CLIENT_SECRET=xxx
#SECOND_GOOGLE_DRIVE_REFRESH_TOKEN=xxx
#SECOND_GOOGLE_DRIVE_FOLDER=backups
#SECOND_DRIVE_TEAM_DRIVE_ID=xxx
Add disks on config/filesystems.php
'disks' => [
// ...
'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
//'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
],
// you can use more accounts, only add more disks and configs on .env
// also you can use the same account and point to a diferent folders for each disk
/*'second_google' => [
'driver' => 'google',
'clientId' => env('SECOND_GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('SECOND_GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('SECOND_GOOGLE_DRIVE_REFRESH_TOKEN'),
'folder' => env('SECOND_GOOGLE_DRIVE_FOLDER'),
],*/
// ...
],
Add driver storage in a ServiceProvider
on path app/Providers/
For Example:
namespace App\Providers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider { // can be a custom ServiceProvider
// ...
public function 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
}
// ...
}
// ...
}
Now you can access the drives like so:
$googleDisk = Storage::disk('google');
//$secondDisk = Storage::disk('second_google'); //others disks
Keep in mind that there can only be one default cloud storage drive, defined by FILESYSTEM_CLOUD
in your .env
(or config) file. If you set it to google, that will be the cloud drive:
Storage::cloud(); // refers to Storage::disk('google')
For more details about this package, you can visit github.
Published at : 04-04-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project