阿里官网文档:/document_detail/32101.html?spm=a2c4g.11186623.6.1093.294d2589CrVykP
需要自己注册阿里云账号并且开通oss服务,建立Bucket存储空间,此步骤不做演示
一、composer安装:使用composer在项目根目录执行以下命令
composer require johnlui/aliyun-oss:~2.0
二、构建 Service 文件需要自己手动建立 app/services/OSS.php
修改配置信息,改为自己的阿里云AccessKeyId AccessKeySecret
注意OSS.php 文件中 private $city = ‘青岛’; 所对应的城市,要根据自己OSS相对应的地区所选择
如何查看自己的OSS地区,请到阿里云OSS中查看,修改错误会导致无法连接到阿里云OSS服务器
OSS.php文件:
<?phpnamespace App\services;use JohnLui\AliyunOSS;use Exception;use DateTime;class OSS {/* 城市名称:** 经典网络下可选:杭州、上海、青岛、北京、张家口、深圳、香港、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜* VPC 网络下可选:杭州、上海、青岛、北京、张家口、深圳、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜*/private $city = '';// 经典网络 or VPCprivate $networkType = '经典网络';private $AccessKeyId = '';private $AccessKeySecret = '';private $ossClient;/*** 私有初始化 API,非 API,不用关注* @param boolean 是否使用内网*/public function __construct($isInternal = false){if ($this->networkType == 'VPC' && !$isInternal) {throw new Exception("VPC 网络下不提供外网上传、下载等功能");}$this->ossClient = AliyunOSS::boot($this->city,$this->networkType,$isInternal,$this->AccessKeyId,$this->AccessKeySecret);}/*** 使用外网上传文件* @param string bucket名称* @param string 上传之后的 OSS object 名称* @param string 上传文件路径* @return boolean 上传是否成功*/public static function publicUpload($bucketName, $ossKey, $filePath, $options = []){$oss = new OSS();$oss->ossClient->setBucket($bucketName);return $oss->ossClient->uploadFile($ossKey, $filePath, $options);}/*** 使用阿里云内网上传文件* @param string bucket名称* @param string 上传之后的 OSS object 名称* @param string 上传文件路径* @return boolean 上传是否成功*/public static function privateUpload($bucketName, $ossKey, $filePath, $options = []){$oss = new OSS(true);$oss->ossClient->setBucket($bucketName);return $oss->ossClient->uploadFile($ossKey, $filePath, $options);}/*** 使用外网直接上传变量内容* @param string bucket名称* @param string 上传之后的 OSS object 名称* @param string 上传的变量* @return boolean 上传是否成功*/public static function publicUploadContent($bucketName, $ossKey, $content, $options = []){$oss = new OSS();$oss->ossClient->setBucket($bucketName);return $oss->ossClient->uploadContent($ossKey, $content, $options);}/*** 使用阿里云内网直接上传变量内容* @param string bucket名称* @param string 上传之后的 OSS object 名称* @param string 上传的变量* @return boolean 上传是否成功*/public static function privateUploadContent($bucketName, $ossKey, $content, $options = []){$oss = new OSS(true);$oss->ossClient->setBucket($bucketName);return $oss->ossClient->uploadContent($ossKey, $content, $options);}/*** 使用外网删除文件* @param string bucket名称* @param string 目标 OSS object 名称* @return boolean 删除是否成功*/public static function publicDeleteObject($bucketName, $ossKey){$oss = new OSS();$oss->ossClient->setBucket($bucketName);return $oss->ossClient->deleteObject($bucketName, $ossKey);}/*** 使用阿里云内网删除文件* @param string bucket名称* @param string 目标 OSS object 名称* @return boolean 删除是否成功*/public static function privateDeleteObject($bucketName, $ossKey){$oss = new OSS(true);$oss->ossClient->setBucket($bucketName);return $oss->ossClient->deleteObject($bucketName, $ossKey);}/*** -------------------------------------------------*** 下面不再分公网内网出 API,也不注释了,大家自行体会吧。。。*** -------------------------------------------------*/public function copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey){$oss = new OSS();return $oss->ossClient->copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey);}public function moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey){$oss = new OSS();return $oss->ossClient->moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey);}// 获取公开文件的 URLpublic static function getPublicObjectURL($bucketName, $ossKey){$oss = new OSS();$oss->ossClient->setBucket($bucketName);return $oss->ossClient->getPublicUrl($ossKey);}// 获取私有文件的URL,并设定过期时间,如 \DateTime('+1 day')public static function getPrivateObjectURLWithExpireTime($bucketName, $ossKey, DateTime $expire_time){$oss = new OSS();$oss->ossClient->setBucket($bucketName);return $oss->ossClient->getUrl($ossKey, $expire_time);}public static function createBucket($bucketName){$oss = new OSS();return $oss->ossClient->createBucket($bucketName);}public static function getAllObjectKey($bucketName){$oss = new OSS();return $oss->ossClient->getAllObjectKey($bucketName);}public static function getObjectMeta($bucketName, $ossKey){$oss = new OSS();return $oss->ossClient->getObjectMeta($bucketName, $ossKey);}}
注意命名空间,如果是按照上述目录则不需修改命名空间,否则修改为与你对应的命名空间
三、在 composer.json 中autoload -> classmap 处增加配置
在终端根目录下进行composer操作:
composer dump-autoload
四、控制器的使用方法
创建控制器类 Api/UploadController 控制器用于接收前台上传的图片,上传到阿里云OSS,并且拿到返回路径。Bucket名称名称换成自己的
<?phpnamespace App\Http\Controllers\Api;use App\Http\Controllers\Controller;use App\Services\OSS;use Illuminate\Http\Request;class UploadController extends Controller{public function upload(Request $request){//获取上传的文件$file = $request->file('file');//获取上传图片的临时地址$tmppath = $file->getRealPath();//生成文件名$fileName = rand(1000,9999) . $file->getFilename() . time() .date('ymd') . '.' . $file->getClientOriginalExtension();//拼接上传的文件夹路径(按照日期格式1810/17/xxxx.jpg)$pathName = date('Y-m/d').'/'.$fileName;//上传图片到阿里云OSSOSS::publicUpload('Bucket名称', $pathName, $tmppath, ['ContentType' => $file->getClientMimeType()]);//获取上传图片的Url链接$Url = OSS::getPublicObjectURL('Bucket名称', $pathName);// 返回状态给前端,Laravel框架会将数组转成JSON格式return ['code' => 0, 'msg' => '上传成功', 'data' => ['src' => $Url]];}}
写路由测试:
然后返回信息和图片的URL:
然后就OK啦