modernmcguire / flysystem-google-drive
Google Drive 的 Flysystem 适配器
1.2.0
2023-02-27 22:45 UTC
Requires
- php: >=8.0
- google/apiclient: ^2.12
- league/flysystem: ^3.2.1
- nao-pon/flysystem-cached-extra: ~1.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ^9.5
README
安装
- 对于 Google Drive API V3
composer require modernmcguire/flysystem-google-drive:~1.1
- 对于 Google Drive API V2 "已弃用"
composer require modernmcguire/flysystem-google-drive:~1.0.0
按照 Google Docs 的说明获取您的 ClientId, ClientSecret & refreshToken,此外,您还可以查看这个 易于遵循的教程
- 将一个
google磁盘添加到您的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'), ], ]
- 创建一个新的 Service Provider (
App\Providers\GoogleDriveServiceProvider.php) 来包含所有的设置逻辑。
<?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); }); } }
- 在
config/app.php文件中将服务提供者添加到 providers 数组中
'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, ]
使用方法
您还可以查看 这个示例 以更好地理解如何直接使用库。
您可以在下面看到如何使用 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');