若是以爲幫助了你,節省了你的時間記得 Star 喲.php
以前用過不少 oss 擴展包可是暫時沒有一個用的順心的,一些擴展包配置太複雜,還有的擴展包不能集成 Laravel 文件驅動,對於喜歡優雅編碼的同窗可能不能忍受laravel
最近閒下來看了一些優秀的開源項目,從中學到很多東西。看了超哥的 overtrue/flysystem-qiniu 後決定本身也寫一個 oss 擴展包.git
$ composer require "iidestiny/flysystem-oss" -vvv
use League\Flysystem\Filesystem; use Iidestiny\Flysystem\Oss\OssAdapter; use Iidestiny\Flysystem\Oss\Plugins\FileUrl; $accessKeyId = 'xxxxxx'; $accessKeySecret = 'xxxxxx'; $endpoint= 'oss.iidestiny.com'; $bucket = 'bucket'; $isCName = true; // 若是 isCname 爲 false,endpoint 應配置 oss 提供的域名如:`oss-cn-beijing.aliyuncs.com`,cname 或 cdn 請自行到阿里 oss 後臺配置並綁定 bucket $adapter = new OssAdapter($accessKeyId, $accessKeySecret, $endpoint, $bucket, $isCName); $flysystem = new Filesystem($adapter);
bool $flysystem->write('file.md', 'contents'); bool $flysystem->write('file.md', 'http://httpbin.org/robots.txt', ['options' => ['xxxxx' => 'application/redirect302']]); bool $flysystem->writeStream('file.md', fopen('path/to/your/local/file.jpg', 'r')); bool $flysystem->update('file.md', 'new contents'); bool $flysystem->updateStream('file.md', fopen('path/to/your/local/file.jpg', 'r')); bool $flysystem->rename('foo.md', 'bar.md'); bool $flysystem->copy('foo.md', 'foo2.md'); bool $flysystem->delete('file.md'); bool $flysystem->has('file.md'); string|false $flysystem->read('file.md'); array $flysystem->listContents(); array $flysystem->getMetadata('file.md'); int $flysystem->getSize('file.md'); string $flysystem->getAdapter()->getUrl('file.md'); string $flysystem->getMimetype('file.md'); int $flysystem->getTimestamp('file.md');
use Iidestiny\Flysystem\Oss\Plugins\FileUrl $flysystem->addPlugin(new FileUrl()); string $flysystem->getUrl('file.md');
該擴展包內部自動依賴了 iidestiny/flysystem-oss
因此兩個包不須要重複安裝,Laravel 用戶直接使用下面這條語句安裝就能夠.github
$ composer require "iidestiny/laravel-filesystem-oss" -vvv
在 config/filesystems.php
添加 oss 配置shell
<?php return [ 'disks' => [ //... 'oss' => [ 'driver' => 'oss', 'access_key' => env('OSS_ACCESS_KEY'), 'secret_key' => env('OSS_SECRET_KEY'), 'endpoint' => env('OSS_ENDPOINT'), 'bucket' => env('OSS_BUCKET'), 'isCName' => env('OSS_IS_CNAME', false), // 若是 isCname 爲 false,endpoint 應配置 oss 提供的域名如:`oss-cn-beijing.aliyuncs.com`,不然爲自定義域名,,cname 或 cdn 請自行到阿里 oss 後臺配置並綁定 bucket ], //... ] ];
## 使用app
<?php $disk = Storage::disk('oss'); // create a file $disk->put('avatars/filename.jpg', $fileContents); // check if a file exists $exists = $disk->has('file.jpg'); // get timestamp $time = $disk->lastModified('file1.jpg'); $time = $disk->getTimestamp('file1.jpg'); // copy a file $disk->copy('old/file1.jpg', 'new/file1.jpg'); // move a file $disk->move('old/file1.jpg', 'new/file1.jpg'); // get file contents $contents = $disk->read('folder/my_file.txt'); // get file url $url = $disk->getUrl('folder/my_file.txt');
更多方法查看 Laravel 文檔 laravel-filesystem-doccomposer