Laravel on SAE use Qiniu
在sae上部署laravel 5>~,并使用七牛云存儲(chǔ)
該博客引用了:夏天,zgldh分享的技術(shù)并加以總結(jié)匯總,在此特別感謝
該博本內(nèi)容使用laravel環(huán)境:laravel-v5.2.15,php-5.6
第一步,安裝七牛云儲(chǔ)存插件
當(dāng)你安裝好laravel環(huán)境或者創(chuàng)建了一個(gè)新的laravel工程時(shí),首先安裝qiniu-laravel-storage擴(kuò)展,使用composer命令:
- composer require zgldh/qiniu-laravel-storage
- config/app.php 里面的 providers 數(shù)組, 加上一行 zgldh\QiniuStorage\QiniuFilesystemServiceProvider
- config/filesystem.php 里面的 disks數(shù)組加上:
'disks' => [
... ,
'qiniu' => [
'driver' => 'qiniu',
'domains' => [
'default' => 'xxxxx.com1.z0.glb.clouddn.com', //你的七牛域名
'https' => 'dn-yourdomain.qbox.me', //你的HTTPS域名
'custom' => 'static.abc.com', //你的自定義域名
],
'access_key'=> '', //AccessKey
'secret_key'=> '', //SecretKey
'bucket' => '', //Bucket名字
'notify_url'=> '', //持久化處理回調(diào)地址
],
],
- 使用方法:
use zgldh\QiniuStorage\QiniuStorage;
$disk = QiniuStorage::disk('qiniu');
$disk->exists('file.jpg'); //文件是否存在
$disk->get('file.jpg'); //獲取文件內(nèi)容
$disk->put('file.jpg',$contents); //上傳文件
$disk->prepend('file.log', 'Prepended Text'); //附加內(nèi)容到文件開頭
$disk->append('file.log', 'Appended Text'); //附加內(nèi)容到文件結(jié)尾
$disk->delete('file.jpg'); //刪除文件
$disk->delete(['file1.jpg', 'file2.jpg']);
$disk->copy('old/file1.jpg', 'new/file1.jpg'); //復(fù)制文件到新的路徑
$disk->move('old/file1.jpg', 'new/file1.jpg'); //移動(dòng)文件到新的路徑
$size = $disk->size('file1.jpg'); //取得文件大小
$time = $disk->lastModified('file1.jpg'); //取得最近修改時(shí)間 (UNIX)
$files = $disk->files($directory); //取得目錄下所有文件
$disk->deleteDirectory($directory); //刪除目錄,包括目錄下所有子文件子目錄
$disk->uploadToken('file.jpg'); //獲取上傳Token
$disk->downloadUrl('file.jpg'); //獲取下載地址
$disk->downloadUrl('file.jpg', 'https'); //獲取HTTPS下載地址
$disk->privateDownloadUrl('file.jpg'); //獲取私有bucket下載地址
$disk->privateDownloadUrl('file.jpg', 'https'); //獲取私有bucket的HTTPS下載地址
$disk->imageInfo('file.jpg'); //獲取圖片信息
$disk->imageExif('file.jpg'); //獲取圖片EXIF信息
$disk->imagePreviewUrl('file.jpg','imageView2/0/w/100/h/200'); //獲取圖片預(yù)覽URL
$disk->persistentFop('file.flv','avthumb/m3u8/segtime/40/vcodec/libx264/s/320x240'); //執(zhí)行持久化數(shù)據(jù)處理
$disk->persistentFop('file.flv','fop','隊(duì)列名'); //使用私有隊(duì)列執(zhí)行持久化數(shù)據(jù)處理
$disk->persistentStatus($persistent_fop_id); //查看持久化數(shù)據(jù)處理的狀態(tài)。
不會(huì)Composer的同學(xué)請(qǐng)自行百度
配置SAE環(huán)境
第一步驟是相對(duì)來說很輕松的,接下來的第二步就比較麻煩了;有興趣的同學(xué)可以參考夏天的博客
以下是個(gè)人總結(jié)的簡單步驟,直接使用即可
- 需要將config目錄下的所有使用env()函數(shù)的參數(shù),修改成絕對(duì)值;因?yàn)閑nv函數(shù)在SAE中無法使用
- 然后SAE環(huán)境的擴(kuò)展包,在源代碼包中的vendor/laravel/framework/src/Illuminate/cloud路徑中,直接將此包拷貝到自己的工程對(duì)應(yīng)目錄下
- 如何使用:修改bootstrap/app.php,添加sae環(huán)境
define('SAE',true);
define('SAE_STORAGE', 'laravel');
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
if(SAE){
$app = new Illuminate\Cloud\SAE\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
Illuminate\Cloud\SAE\Kernel::class
);
}
return $app;
- 修改config文件下的相關(guān)sae環(huán)境變量,具體請(qǐng)參考源碼包中config下的文件
-
需要注意:以上過程大體與夏天博客無異,且在不使用七牛云儲(chǔ)存的情況下可直接使用,但是需要同時(shí)使用七牛云儲(chǔ)存時(shí)需要注意一處關(guān)鍵步驟:
在config/app.php文件中:請(qǐng)注釋掉Storage類,使用原來的Storage函數(shù),才可調(diào)用七牛云儲(chǔ)存
if(SAE){
$removeProviders = [
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
];
for($i = 0; $i < count($app['providers']); $i++){
if (in_array($app['providers'][$i], $removeProviders)) {
unset($app['providers'][$i]);
}
}
$app['providers'] = array_merge($app['providers'],[
Illuminate\Cloud\SAE\Cache\SaeCacheServiceProvider::class,
Illuminate\Cloud\SAE\Session\SessionServiceProvider::class,
Illuminate\Cloud\SAE\Storage\StorageServiceProvider::class,
Illuminate\Cloud\SAE\Segment\SegmentServiceProvider::class,
]);
// $app['aliases']['Storage'] = Illuminate\Cloud\SAE\Storage\Storage::class;//注釋此行才可使用七牛云儲(chǔ)存
$app['aliases']['Segment'] = Illuminate\Cloud\SAE\Segment\Segment::class;
}
- 擴(kuò)展如果有自己喜歡研究適配的可以重寫cloud/sae中的Storage方法,并且修改QiniuFilesystemServiceProvider中的Storage調(diào)用關(guān)系,或者自己重新修改Qiniu擴(kuò)展協(xié)議
源碼下載地址:Github
我的個(gè)人博客