yaza / laravel-google-drive-storage
这是我开发的laravel-google-drive-storage包
V3.0.0
2024-05-11 13:51 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- masbug/flysystem-google-drive-ext: ^2.2
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2024-09-11 14:48:29 UTC
README
此包允许像S3 AWS一样在Laravel中存储和获取Google Drive中的数据
支持
- Laravel 11 (php 8.2) 使用 V3.x
- Laravel 10 (php 8.1) 使用 V2.0.0
安装
您可以通过composer安装此包
composer require yaza/laravel-google-drive-storage
复制到.env文件
FILESYSTEM_CLOUD=google GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com GOOGLE_DRIVE_CLIENT_SECRET=xxx GOOGLE_DRIVE_REFRESH_TOKEN=xxx GOOGLE_DRIVE_FOLDER=
配置filesystem.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'), ] ]
设置Google密钥
用法
您可以使用Laravel的存储驱动功能
示例
Storage::disk('google')->put($filename, File::get($filepath));
参考代码 示例代码
或使用此包的辅助函数
- 上传文件
use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::put('location/filename.png', $request->file('file')); // or Gdrive::put('filename.png', public_path('path/filename.png'));
- 获取文件
use Yaza\LaravelGoogleDriveStorage\Gdrive; $data = Gdrive::get('path/filename.png'); return response($data->file, 200) ->header('Content-Type', $data->ext);
- 通过流获取大文件
use Yaza\LaravelGoogleDriveStorage\Gdrive; $readStream = Gdrive::readStream('path/filename.png'); return response()->stream(function () use ($readStream) { fpassthru($readStream->file); }, 200, [ 'Content-Type' => $readStream->ext, //'Content-disposition' => 'attachment; filename="'.$filename.'"', // force download? ]);
- 下载文件
use Yaza\LaravelGoogleDriveStorage\Gdrive; $data = Gdrive::get('path/filename.png'); return response($data->file, 200) ->header('Content-Type', $data->ext) ->header('Content-disposition', 'attachment; filename="'.$data->filename.'"');
- 删除
use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::delete('path/filename.png');
- 删除目录
use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::deleteDir('foldername');
- 创建目录
use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::makeDir('foldername');
- 重命名目录
use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::renameDir('oldfolderpath', 'newfolder');
- 所有文件夹和文件
use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::all('/'); // or Gdrive::all('foldername');
输出
Illuminate\Support\Collection {#804 ▼ // app/Http/Controllers/UploadController.php:70 #items: array:3 [▼ 0 => League\Flysystem\DirectoryAttributes {#798 ▶} 1 => League\Flysystem\FileAttributes {#796 ▶} 2 => League\Flysystem\DirectoryAttributes {#783 ▶} ] #escapeWhenCastingToString: false }
- 所有文件夹和文件及其子文件夹
use Yaza\LaravelGoogleDriveStorage\Gdrive; Gdrive::all('/', true); // or Gdrive::all('foldername', true);
输出
Illuminate\Support\Collection {#804 ▼ // app/Http/Controllers/UploadController.php:70 #items: array:3 [▼ 0 => League\Flysystem\DirectoryAttributes {#798 ▶} 1 => League\Flysystem\FileAttributes {#796 ▶} 2 => League\Flysystem\DirectoryAttributes {#783 ▶} ] #escapeWhenCastingToString: false }
限制
使用显示路径作为文件夹和文件的标识符时,需要它们是唯一的。不幸的是,Google Drive允许用户创建具有相同(显示)名称的文件和文件夹。在这种情况下,当无法确定唯一路径时,此适配器会选择最旧的(第一个)实例。如果较新的重复项是文件夹,并且用户在适配器内部放置了唯一的文件或文件夹,适配器将能够正确地访问它(因为完整路径是唯一的)。
更新日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
致谢
感谢 Masbug
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件。