PHP code example of masbug / flysystem-google-drive-ext
1. Go to this page and download the library: Download masbug/flysystem-google-drive-ext library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
masbug / flysystem-google-drive-ext example snippets
$client = new \Google\Client();
$client->setClientId([client_id]);
$client->setClientSecret([client_secret]);
$client->refreshToken([refresh_token]);
$client->setApplicationName('My Google Drive App');
$service = new \Google\Service\Drive($client);
// variant 1
$adapter = new \Masbug\Flysystem\GoogleDriveAdapter($service, 'My_App_Root');
// variant 2: with extra options and query parameters
$adapter2 = new \Masbug\Flysystem\GoogleDriveAdapter(
$service,
'My_App_Root',
[
'useDisplayPaths' => true, /* this is the default */
/* These are global parameters sent to server along with per API parameters. Please see https://cloud.google.com/apis/docs/system-parameters for more info. */
'parameters' => [
/* This example tells the remote server to perform quota checks per unique user id. Otherwise the quota would be per client IP. */
'quotaUser' => (string)$some_unique_per_user_id
]
]
);
// variant 3: connect to team drive
$adapter3 = new \Masbug\Flysystem\GoogleDriveAdapter(
$service,
'My_App_Root',
[
'teamDriveId' => '0GF9IioKDqJsRGk9PVA'
]
);
// variant 4: connect to a folder shared with you
$adapter4 = new \Masbug\Flysystem\GoogleDriveAdapter(
$service,
'My_App_Root',
[
'sharedFolderId' => '0GF9IioKDqJsRGk9PVA'
]
);
$fs = new \League\Flysystem\Filesystem($adapter, new \League\Flysystem\Config([\League\Flysystem\Config::OPTION_VISIBILITY => \League\Flysystem\Visibility::PRIVATE]));
// List selected root folder contents
$contents = $fs->listContents('', true /* is_recursive */);
// List specific folder contents
$contents = $fs->listContents('MyFolder', true /* is_recursive */);
'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'),
//'sharedFolderId' => env('GOOGLE_DRIVE_SHARED_FOLDER_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'),
],*/
// ...
],
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'];
}
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
}
// ...
}
// ...
}