// +---------------------------------------------------------------------- namespace App\Services; use Qiniu\Auth; use Qiniu\Storage\BucketManager; use Qiniu\Storage\UploadManager; /** * 七牛存储接口管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services */ class QiniuService extends BaseService { // 静态对象 protected static $instance = null; protected $host = ''; protected $accessKey = ''; protected $secretKey = ''; protected $bucket = ''; public function __construct() { $this->accessKey = ConfigService::make()->getConfigByCode('qiniu_access_key'); $this->secretKey = ConfigService::make()->getConfigByCode('qiniu_secret_key'); $this->bucket = ConfigService::make()->getConfigByCode('qiniu_bucket', ''); $this->host = ConfigService::make()->getConfigByCode('qiniu_host', ''); } /** * 静态入口 */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 上传文件/图片 * @param $filename * @param $file * @return false|string * @throws \Exception */ public function upload($filename, $file) { // 配置参数 if (empty($this->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)) { $this->error = 'QINIU接口参数未配置'; return false; } // 初始化鉴权对象 $auth = new Auth($this->accessKey, $this->secretKey); // 生成上传策略 $policy = null; $uptoken = $auth->uploadToken($this->bucket, $filename, 3600, $policy); // 初始化 UploadManager 对象并进行文件上传 $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr->putFile($uptoken, $filename, $file->getPathname()); if ($err !== null) { $this->error = 'QINIU文件上传失败:' . (is_object($err) ? $err->getMessage() : $err); return false; } else { return '/qiniu/' . $filename; } } /** * 上传base图片 * @param $filename * @param $base64 * @return false|string */ public function uploadBase64($filename, $base64) { // 配置参数 if (empty($this->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)) { $this->error = 'QINIU接口参数未配置'; return false; } // 初始化鉴权对象 $auth = new Auth($this->accessKey, $this->secretKey); // 生成上传策略 $policy = null; $uptoken = $auth->uploadToken($this->bucket, $filename, 3600, $policy); // 初始化 UploadManager 对象并进行文件上传 $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr->put($uptoken, $filename, $base64, null, 'image/png'); if ($err !== null) { $this->error = 'QINIU文件上传失败'; return false; } else { return '/qiniu/' . $filename; } } public function deleteImage($url) { // 配置参数 if (empty($this->accessKey) || empty($this->secretKey) || empty($this->bucket) || empty($this->host)) { $this->error = 'QINIU接口参数未配置'; return false; } // 初始化鉴权对象 $auth = new Auth($this->accessKey, $this->secretKey); // 初始化 UploadManager 对象并进行文件上传 $bucketManager = new BucketManager($auth); $res = $bucketManager->delete($this->bucket, $url); if ($res === null) { $this->error = '删除成功'; return true; } else { $this->error = '删除失败'; return false; } } /** * 图片路径 * @param $path * @return string|string[] */ public function getImageUrl($path) { return str_replace('/qiniu', $this->host, $path); } /** * 图片路径 * @param $path * @return string|string[] */ public function getImagePath($path) { return str_replace($this->host, '/qiniu', $path); } }