在laravel应用项目中实现阿里云OSS文件上传及签名流程步骤
oss服务在开发者是非常普遍了,国内很多云服务商都有该功能服务,下面看看阿里云的oss
项目环境
laravel + aliyun oss
示例功能
在laravel项目中实现oss文件上传及签名验签功能
进入步骤
1.composer安装阿里云oss
composer require aliyuncs/oss-sdk-php
ps:
需注意包版本,是否与 php 版本适用。
2.配置文件.env
# OSS相关配置
OSS_DRIVER=AL_OSS
#阿里云
OSS_ENDPOINT=https://oss-cn-hangzhou.aliyuncs.com
OSS_KEYID=xx
OSS_KEYSECRET=xx
OSS_BUCKET=xx
3.接口声明
<?php
namespace App\Service\OSS;
interface IOSS
{
/**
* 上传
*
* @param $fullFileName
* @param $filePath
* @return mixed
*/
public function publicUpload($fullFileName, $filePath);
/**
* url验签、下载
*
* @param $fullFileName | 含前缀的完整url文件名
* @param $expires | 过期时效
* @return mixed
*/
public function getUrl($fullFileName, $expires);
/**
* 可替换url域名
*
* @param $url
* @return mixed
*/
public function replaceUrl($url);
}
4.阿里OSS实现
<?php
namespace App\Service\OSS;
use OSS\OssClient;
class AliOSS implements IOSS
{
private $endPoint;
private $keyId;
private $secret;
private $bucket;
private $ossClient;
private $expires = 3 * 24 * 3600;
private $aliHost = '';
private $myHost = '';
public function __construct()
{
$this->endPoint = env("OSS_ENDPOINT");
$this->keyId = env("OSS_KEYID");
$this->secret = env("OSS_KEYSECRET");
$this->bucket = env("OSS_BUCKET");
try {
$this->ossClient = new OssClient($this->keyId, $this->secret, $this->endPoint);
} catch (\Exception $e) {
}
}
/**
* 上传
*
* @param $fullFileName
* @param $filePath
* @return mixed
* @throws \Exception
*/
public function publicUpload($fullFileName, $filePath)
{
return $this->ossClient->uploadFile($this->bucket, $fullFileName, $filePath);
}
/**
* url验签、下载
*
* @param $fullFileName
* @param $expires | 过期时效
* @return mixed
* @throws \Exception
*/
public function getUrl($fullFileName, $expires)
{
$expires = $expires ? $expires : $this->expires;
$signUrl = $this->ossClient->signUrl($this->bucket, $fullFileName, $expires);
return $signUrl;
}
/**
* 替换url域名
*
* @param $url
* @return mixed
*/
public function replaceUrl($url)
{
return str_replace($this->aliHost, $this->myHost, $url);
}
}
4.阿里云OSS类
<?php
namespace App\Service;
use App\Service\OSS\AliOSS;
use Exception;
class OSS
{
const DEFAULT_DRIVER = 'AL_OSS';
const OSS_PREFIX = 'oss/';
public $OSSService;
/**
* 初始化 service
*/
public function __construct()
{
$this->OSSService = new AliOSS();
}
public static function getInstance()
{
return new self();
}
/**
* 使用外网上传文件
*
* @param $fullName
* @param $filePath
* @param $prefix
* @return mixed
* @throws Exception
*/
public static function publicUpload($fullName, $filePath, $prefix)
{
return self::getInstance()->OSSService->publicUpload($fullName, $filePath);
}
/**
* 获取oss图片url
*
* @param $fullName
* @param $expires | 过期时效
* @return string
* @throws Exception
*/
public static function getUrl($fullName, $expires = '')
{
return self::getInstance()->OSSService->getUrl($fullName, $expires);
}
/**
* 替换url域名
*
* @param $url
* @return mixed
*/
public static function replaceUrl($url)
{
return self::getInstance()->OSSService->replaceUrl($url);
}
/**
* 获取完整的文件名含路径
*
* @param $fileName
* @param $prefix
* @return string
*/
public static function getFullFileName($fileName, $prefix)
{
return self::OSS_PREFIX . $prefix . self::setFileName($fileName);
}
/**
* 设置新的文件名(重命名规则)
*
* @param $fileName
* @return string
*/
public static function setFileName($fileName)
{
$nameArray = explode('.', $fileName);
$extension = $nameArray[count($nameArray) - 1];
$newName = date('Ymd') . '/' . date('YmdHis') . rand(10000, 99999) . '.' . $extension;
return $newName;
}
}
5.文件上传类
<?php
namespace App\Service;
class UploadFile
{
/**
* 文件上传 带签名访问
*
* @param $files
* @param string $prefix
* @return array
* @throws \Exception
*/
public static function upload($files, $prefix = '')
{
if (empty($files)) {
return ['ok' => false, 'message' => '请上传文件!'];
}
if (is_array($files)) {
$pics = [];
foreach ($files as $key => $file) {
if ($file->isValid()) {
$name = $file->getClientOriginalName();
$fullName = OSS::getFullFileName($name, $prefix);
$ret = OSS::publicUpload($fullName, $file, $prefix);
if ($ret) {
$url = OSS::getUrl($fullName);
$url = OSS::replaceUrl($url);
$pics[] = ['name' => $name, 'url' => $url, 'file_name' => $fullName];
}
} else {
return ['ok' => false, 'message' => '无效文件!'];
}
}
if (count($pics) > 0) {
return ['ok' => true, 'data' => $pics];
}
} else {
$name = $files->getClientOriginalName();
$fullName = OSS::getFullFileName($name, $prefix);
$ret = OSS::publicUpload($fullName, $files, $prefix);
if ($ret) {
$url = OSS::getUrl($fullName);
$url = OSS::replaceUrl($url);
return ['ok' => true, 'data' => ['name' => $name, 'url' => $url, 'file_name' => $fullName]];
} else {
return ['ok' => false, 'message' => '无效文件!'];
}
}
}
}
相关文章