jervi-sir / flysystem-gdrive-api
Google Drive 的 Flysystem 适配器
dev-master / 1.1.x-dev
2021-07-06 23:12 UTC
Requires
- php: >=5.4.0
- google/apiclient: ^2.0
- league/flysystem: ~1.0
- nao-pon/flysystem-cached-extra: ~1.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-07 06:31:09 UTC
README
注意
此项目来自 nao-pon/由于无法直接编辑供应商文件,我在这里克隆了它,以对紧急项目进行一些更改
因此,所有努力都归功于 nao-pon/
在此处检查原始仓库# Google Drive 的 Flysystem 适配器
安装
步骤将与原始仓库不同。
composer require spatie/laravel-backup 7.3.3
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
- 然后在 config/backup.php 中添加以下内容
'disks' => [
'google',
'local',
],
composer require jervi-sir/flysystem-gdrive-api:~1.1
php artisan make:provider GoogleDriveServiceProvider
- 在 app/Providers/GoogleDriveServiceProvider.php 中,也在 config/app.php 文件中声明。
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);
$adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);
return new \League\Flysystem\Filesystem($adapter);
});
}
- 在 config/app.php 中声明 google 提供者
App\Providers\GoogleDriveServiceProvider::class,
- 在 config/filesystem.php 中
return [
// ...
'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'),
],
// ...
],
// ...
];
- 在 .env 中
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER_ID=null
获取 Google 凭据
查看这篇文章 如何在 Google Drive 上设置 Laravel 备份?
然后执行以下 bash 命令
php artisan backup:run
用法
- 在控制器中
//Create a disk
$disk = Storage::disk('google');
//Create Image Object (Image Intervention package must be installed)
$img = Image::make($image)->orientate()->encode('jpg', 50);
$img->encode();
//Upload file
$disk->put($file_name, $img->getEncoded());
//get the Id
$pic_id = $disk->getDriver()->getAdapter()->getCacheFileObjectsByName()->id;
备注
注意,在前面的代码中,我调用了 getCacheFileObjectsByName() 函数,这是我需要为此包进行的唯一更改,以便访问私有变量 CacheFileObjectsByName,这样我就可以获取上传文件的 id
- 以下是 GoogleDriveAdapter.php 中的更改,我添加了
public function getCacheFileObjectsByName()
{
return array_values($this->cacheFileObjectsByName)[0];
}