laravel下封装的七牛云图片上传下载方法
$fileName 文件名
$filePath 文件路径
我用的是七牛云私有空间,如果用公有空间也一样,只是不需要下载方法而已。
<?php namespace App\Http\Controllers; use Qiniu\Auth; use Qiniu\Storage\BucketManager; use Qiniu\Storage\UploadManager; class QiniuApiController extends Controller { public $auth; public $bucket; public $uploadManager; public $backetManager; public function __construct() { $this->auth = new Auth(env('ACCESS_KEY'),env('SECRET_KEY')); $this->bucket = env('BUCKET'); $this->uploadManager = new UploadManager(); $this->backetManager = new BucketManager($this->auth); } /** * 上传文件到七牛云 * @param $fileName 文件名 * @param $filePath 文件路径 * @return mixed * @throws \Exception */ public function uploadFile($fileName,$filePath) { $token = $this->auth->uploadToken($this->bucket); list($res,$err) = $this->uploadManager->putFile($token,$fileName,$filePath); if ($err !== null){ return $err; }else{ return $res; } } /** * 下载七牛云的数据,并添加限时token * @param $fileName 传入需要下载的文件名 * @return string 返回链接字符串 */ public function downloadFile($fileName) { if(strpos($fileName,'http://') === false && strpos($fileName,env('QINIU_DOMAIN')) ===false){ $baseUrl = 'http://'.env('QINIU_DOMAIN').'/'.$fileName; }else if (strpos($fileName,'http://') === false){ $baseUrl = 'http://'.$fileName; }else{ $baseUrl = $fileName; } $authUrl = $this->auth->privateDownloadUrl($baseUrl); return $authUrl; }