modernmcguire/flysystem-google-drive

Flysystem adapter for Google Drive

1.2.0 2023-02-27 22:45 UTC

README

Author Software License

Installation

  • For Google Drive API V3
composer require modernmcguire/flysystem-google-drive:~1.1
  • For Google Drive API V2 "Deprecated"
composer require modernmcguire/flysystem-google-drive:~1.0.0

Follow Google Docs to obtain your ClientId, ClientSecret & refreshToken in addition you can also check this easy-to-follow tutorial

  1. Add a google disk to your 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'),
		'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
		'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
	],
]
  1. Create a new Service Provider (App\Providers\GoogleDriveServiceProvider.php) to contain all of the setup logic.
<?php

namespace App\Providers;

use Google_Client;
use Google_Service_Drive;
use League\Flysystem\Filesystem;
use App\Http\Livewire\GoogleDriveUI;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;

class GoogleDriveServiceProvider extends ServiceProvider
{
	public function register()
	{
		//
	}

	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);

			$options = [];
			if (isset($config['teamDriveId'])) {
				$options['teamDriveId'] = $config['teamDriveId'];
			}

			$options['additionalFetchField'] = 'thumbnailLink,contentHints,webContentLink,webViewLink,iconLink';

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

			if (isset($config['teamDriveId']) && ! empty($config['teamDriveId'])) {
				// Reset the pathPrefix and root back to your custom parent folder
				$adapter->setPathPrefix($config['folderId']);
				$adapter->root = $config['folderId'];
			}

			// if we want to implement Flysystem caching
			// $cacheStore = new \League\Flysystem\Cached\Storage\Memory();
			// $adapter = new \League\Flysystem\Cached\CachedAdapter($adapter, $cacheStore);
			// return new Filesystem($adapter);

			return new Filesystem($adapter);
		});
	}
}
  1. Add the service provider to your providers array in config/app.php
'providers' => [

	/* ... */

	/*
     * Application Service Providers...
     */
	App\Providers\AppServiceProvider::class,
	App\Providers\AuthServiceProvider::class,
	App\Providers\BladeServiceProvider::class,
	// App\Providers\BroadcastServiceProvider::class,
	App\Providers\EventServiceProvider::class,

	App\Providers\GoogleDriveServiceProvider::class,


]

Usage

You can also check This Example for a better understanding of how to use the library directly.

You can see below for the indirect usage utilizing the Laravel Filesystem.

// get folders and files beneath a parent folder
Storage::disk('google')->listContents('/', $recursive = true);

Storage::disk('google')->get($path);
Storage::disk('google')->put($path, 'Some contents');

Storage::disk('google')->download($path, $filename);

Storage::disk('google')->delete($path);
Storage::disk('google')->deleteDirectory($path);

Storage::disk('google')->makeDirectory($path . '/Some new folder');

Storage::disk('google')->copy($source, $destination);
Storage::disk('google')->move($source, $destination);

Storage::disk('google')->setVisibility($path, 'public');
Storage::disk('google')->setVisibility($path, 'private');

Tips