123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace crmeb\services\storage;
- use Guzzle\Http\EntityBody;
- use Qcloud\Cos\Client;
- use think\facade\Cache;
- /**
- * TODO 腾讯云COS文件上传
- * Class COS
- */
- class COS
- {
- protected static $accessKey;
- protected static $secretKey;
- protected static $auth = null;
- //TODO 空间域名 Domain
- protected static $uploadUrl;
- //TODO 存储空间名称 公开空间
- protected static $storageName;
- //TODO COS使用 所属地域
- protected static $storageRegion;
- /**
- * TODO 初始化
- * @return null|Client
- * @throws \Exception
- */
- protected static function autoInfo()
- {
- if (($storageRegion = Cache::get('storageRegion')) && ($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))) {
- self::$accessKey = $accessKey;
- self::$secretKey = $secretKey;
- self::$uploadUrl = $uploadUrl;
- self::$storageName = $storageName;
- self::$storageRegion = $storageRegion;
- } else {
- self::$accessKey = trim(sys_config('accessKey'));
- self::$secretKey = trim(sys_config('secretKey'));
- self::$uploadUrl = trim(sys_config('uploadUrl')) . '/';
- self::$storageName = trim(sys_config('storage_name'));
- self::$storageRegion = trim(sys_config('storage_region'));
- Cache::set('accessKey', self::$accessKey);
- Cache::set('secretKey', self::$secretKey);
- Cache::set('uploadUrl', self::$uploadUrl);
- Cache::set('storageName', self::$storageName);
- Cache::set('storageRegion', self::$storageRegion);
- }
- if (!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName) {
- exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
- }
- if (self::$auth == null) {
- self::$auth = new Client([
- 'region' => self::$storageRegion,
- 'credentials' => [
- 'secretId' => self::$accessKey,
- 'secretKey' => self::$secretKey,
- ]
- ]);
- }
- return self::$auth;
- }
- /**
- * TODO 文件上传 名称
- * @param string $filename
- * @return string
- */
- public static function uploadImage($filename = 'image')
- {
- $request = app('request');
- $file = $request->file($filename);
- $filePath = $file->getRealPath();
- $ext = $file->getOriginalExtension();
- $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
- try {
- self::autoInfo();
- return [self::$uploadUrl . $key, self::$auth->putObject([
- 'Bucket' => self::$storageName,
- 'Key' => $key,
- 'Body' => fopen($filePath, 'rb')
- ])];
- } catch (\Exception $e) {
- return [false, $e->getMessage()];
- }
- }
- /**
- * TODO 文件上传 内容
- * @param $key
- * @param $content
- * @return string
- */
- public static function uploadImageStream($key, $content)
- {
- try {
- self::autoInfo();
- return [self::$uploadUrl . $key, self::$auth->putObject([
- 'Bucket' => self::$storageName,
- 'Key' => $key,
- 'Body' => $content
- ])];
- } catch (\Exception $e) {
- return [false, $e->getMessage()];
- }
- }
- /**
- * TODO 删除资源
- * @param $key
- * @return mixed
- */
- public static function delete($key)
- {
- try {
- self::autoInfo();
- return self::$auth->deleteObject([
- 'Bucket' => self::$storageName,
- 'Key' => $key
- ]);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * TODO 转为文件流
- * @param $resource
- * @return EntityBody
- */
- public static function resourceStream($resource)
- {
- return EntityBody::factory($resource)->__toString();
- }
- }
|