解决GoogleDrive文件路径混乱问题:使用flysystem-google-drive-ext实现无缝路径转换
可以通过一下地址学习作曲家:学习地址
在使用googledrive作为程序的存储云端时,我们很快会遇到一个问题:googledrive使用唯一的id来标识每个文件和文件夹,而不是像传统文件系统那样使用文本的路径。这给集成带来了很多麻烦,我们需要在应用程序中维护这些id,并手动进行路径转换。
例如,我们可能需要在应用程序中显示一个易于理解的路径/My Nice Dir/myFile.ext,但在 Google Drive 中,实际的“虚拟路径”却是 /Xa3X9GlR6EmbnY1RLVTk5VUtOVkk/0B3X9GlR6EmbnY1RLVTk5VUtOVkk。手动处理这些转换不仅麻烦,而且很容易出错。
masbug/flysystem-google-drive-ext 扩展包就是为了解决这个问题而生的。它是一个 Flysystem一对,能够无缝地在“显示路径”和“虚拟路径”之间进行转换,隐藏了Google Drive的内部ID 管理,我们可以像使用传统文件系统一样操作Google Drive。
安装
使用Composer安装非常简单:composer require masbug/flysystem-google-drive-ext登录后复制
配置
首先,你需要从Google获取客户端ID、客户端密钥和刷新令牌。这些初步用于授权你的应用程序访问Google Drive。具体步骤可以参考Google官方文档。
获取到发票后,就可以使用 Masbug\Flysystem\GoogleDriveAdapter 创建预备实例了:use Masbug\Flysystem\GoogleDriveAdapter;use Google\Client;use Google\Service\Drive;use League\Flysystem\Filesystem;use League\Flysystem\Config;use League\Flysystem\Visibility;$client = new Client();$client-gt;setClientId('[client_id]');$client-gt;setClientSecret('[client_secret]');$client-gt;refreshToken('[refresh_token]');$client-gt;setApplicationName('My Google Drive App');$service = new Drive($client);//适配器$adapter = new GoogleDriveAdapter($service, 'My_App_Root');// 创建 Flysystem 实例$fs = new Filesystem($adapter, new Config([Config::OPTION_VISIBILITY =gt; Visibility::PRIVATE]));登录后复制
在上面的代码中,My_App_Root 指定了应用程序在 Google Drive 中使用的根目录。
使用
现在,你可以像使用其他任何东西一样Flysystem 和使用 Google Drive 一样。
例如,入门根目录下的所有文件和文件夹:$contents = $fs-gt;listContents('', true /* is_recursive */);登录后复制
上传文件:use League\Flysystem\Local\LocalFilesystemAdapter;use Carbon\Carbon;use League\Flysystem\UnableToWriteFile;$local_filepath = '/home/user/downloads/file_to_upload.ext';$remote_filepath = 'MyFolder/file.ext';$localAdapter = new LocalFilesystemAdapter('/');$localfs = new Filesystem($localAdapter, [Config::OPTION_VISIBILITY =gt;可见性::PRIVATE]);try { $time = Carbon::now(); $fs-gt;writeStream($remote_filepath, $localfs-gt;readStream($local_filepath), new Config()); $speed = !(float)$time-gt;diffInSeconds() ? 0 :filesize($local_filepath) / (float)$time-gt;diffInSeconds(); echo '已用时间: '.$time-gt;diffForHumans(null, true).PHP_EOL; echo '速度: '. number_format($速度/1024,2) . ' KB/s'.PHP_EOL;} catch(UnableToWriteFile $e) { echo 'UnableToWriteFile!'.PHP_EOL.$e-gt;getMessage();}登录后复制
下载文件:use League\Flysystem\Local\LocalFilesystemAdapter;use Carbon\Carbon;use League\Flysystem\UnableToWriteFile;$remote_filepath = 'MyFolder/file.ext';$local_filepath = '/home/user/downloads/file.ext';$localAdapter = new LocalFilesystemAdapter('/');$localfs = new Filesystem($localAdapter, [Config::OPTION_VISIBILITY =gt;可见性::PRIVATE]);try { $time = Carbon::now(); $localfs-gt;writeStream($local_filepath, $fs-gt;readStream($remote_filepath), new Config()); $spe
ed = !(float)$time-gt;diffInSeconds() ? 0 :filesize($local_filepath) / (float)$time-gt;diffInSeconds(); echo '已用时间: '.$time-gt;diffForHumans(null, true).PHP_EOL; echo '速度: '. number_format($速度/1024,2) . ' KB/s'.PHP_EOL;} catch(UnableToWriteFile $e) { echo 'UnableToWriteFile!'.PHP_EOL.$e-gt方便;getMessage();}登录后复制
在 Laravel 中使用
masbug/flysystem-google-drive-ext 也可以很本地集成到 Laravel 框架中。
配置.env文件:
在.env文件中添加Google Drive的凭据:FILESYSTEM_CLOUD=googleGOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.comGOOGLE_DRIVE_CLIENT_SECRET=xxxGOOGLE_DRIVE_REFRESH_TOKEN=xxxGOOGLE_DRIVE_FOLDER=登录后复制
配置config/filesystems.php 文件:
在 config/filesystems.php 文件中添加一个名为 google 的磁盘:'disks' =gt; [ // ... 'google' =gt; [ 'driver' =gt; 'google', 'clientId' =gt; env('GOOGLE_DRIVE_CLIENT_ID'), 'clientSecret' =gt; env('GOOGLE_DRIVE_CLIENT_SECRET'), 'refreshToken' =gt; env('GOOGLE_DRIVE_REFRESH_TOKEN'), 'folder' =gt; env('GOOGLE_DRIVE_FOLDER'), // without folder is root of drive or team drive ], // ...],登录后复制
注册服务提供商:
在 app/Providers/AppServiceProvider.php 文件中,添加以下代码:namespace App\Providers;use Illuminate\Support\Facades\Storage;use Illuminate\Support\ServiceProvider;use League\Flysystem\Filesystem;use Masbug\Flysystem\GoogleDriveAdapter;use Google\Client;use Google\Service\Drive;use Illuminate\Filesystem\FilesystemAdapter;class AppServiceProvider extends ServiceProvider{ public function boot() { 存储::extend('google', function ($app, $config) { $client = new Client(); $client-gt;设置客户端ID($config['clientId']); $client-gt;设置客户端秘密($config['clientSecret']); $client-gt;刷新令牌($config['refreshToken']); $servi
ce = new Drive($client); $adapter = new GoogleDriveAdapter($service, $config['folder'] ?? '/'); $driver = new Filesystem($adapter); return new FilesystemAdapter($driver, $adapter); }); }}登录复制后
现在,你可以像这样访问 Google Drive:use Illuminate\Support\Facades\Storage;$googleDisk = Storage::disk('google');// 上传文件$googleDisk-gt;put('MyFolder/file.ext', file_get_contents('/path/to/local/file.ext'));// 下载文件$contents = $googleDisk-gt;get('MyFolder/file.ext');登录后复制
优势无缝路径转换:自动处理Google Drive虚拟路径和显示路径之间的转换,简化了开发过程。 易于集成:可以轻松地集成到现有的 Flysystem代码中。支持Team Drive:支持连接到Team Drive,方便团队协作。支持共享文件夹:支持连接到与您共享的文件夹。流式上传/下载:支持直接从流中上传和下载文件,避免将数据复制到内存中,提高了性能。
相邻重复文件名:Google Drive允许用户创建显示相同名称的文件和文件夹。在这种情况下,适配器会选择最新的实例。使用ARM:可能会导致意外问题,因为文件/文件夹标识符和文件对象会被大量缓存。
总结
masbug/flysystem-google-drive-ext扩展包是一个强大的工具,可以帮助我们更轻松地导出应用程序与Google Drive集成。通过无缝的路径转换,简化了文件管理,提高了开发效率。如果您正在使用Google Drive作为应用程序的存储路径,那么这个扩展包绝对值得一试。
以上就是解决GoogleDrive文件路径混乱问题:使用flysystem-google-drive-ext实现路径转换的详细内容,更多请关注乐哥常识网文章相关其他!