正在显示
3 个修改的文件
包含
111 行增加
和
0 行删除
| @@ -6,6 +6,7 @@ use App\Enums\Common\Code; | @@ -6,6 +6,7 @@ use App\Enums\Common\Code; | ||
| 6 | use App\Http\Controllers\Controller; | 6 | use App\Http\Controllers\Controller; |
| 7 | use App\Http\Controllers\type; | 7 | use App\Http\Controllers\type; |
| 8 | use App\Models\File\Image as ImageModel; | 8 | use App\Models\File\Image as ImageModel; |
| 9 | +use App\Services\TencentCosService; | ||
| 9 | use Illuminate\Http\Exceptions\HttpResponseException; | 10 | use Illuminate\Http\Exceptions\HttpResponseException; |
| 10 | use Illuminate\Http\JsonResponse; | 11 | use Illuminate\Http\JsonResponse; |
| 11 | use Illuminate\Support\Facades\Cache; | 12 | use Illuminate\Support\Facades\Cache; |
| @@ -164,6 +165,7 @@ class ImageController extends Controller | @@ -164,6 +165,7 @@ class ImageController extends Controller | ||
| 164 | if ($rs === false) { | 165 | if ($rs === false) { |
| 165 | return $this->response('添加失败', Code::USER_ERROR); | 166 | return $this->response('添加失败', Code::USER_ERROR); |
| 166 | } | 167 | } |
| 168 | + return $this->uploadCos($this->path,$files->getClientOriginalExtension()); | ||
| 167 | return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); | 169 | return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); |
| 168 | } | 170 | } |
| 169 | 171 | ||
| @@ -317,4 +319,16 @@ class ImageController extends Controller | @@ -317,4 +319,16 @@ class ImageController extends Controller | ||
| 317 | $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); | 319 | $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); |
| 318 | } | 320 | } |
| 319 | } | 321 | } |
| 322 | + | ||
| 323 | + /** | ||
| 324 | + * @remark :上传文件到cos | ||
| 325 | + * @name :uploadCos | ||
| 326 | + * @author :lyh | ||
| 327 | + * @method :post | ||
| 328 | + * @time :2023/7/18 17:38 | ||
| 329 | + */ | ||
| 330 | + public function uploadCos($path,$image_type){ | ||
| 331 | + $txCos = new TencentCosService(); | ||
| 332 | + return $txCos->upload_image($path,$image_type); | ||
| 333 | + } | ||
| 320 | } | 334 | } |
app/Services/TencentCosService.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Services; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * @remark :腾讯cos上传 | ||
| 7 | + * @class :TencentCosService.php | ||
| 8 | + * @author :lyh | ||
| 9 | + * @time :2023/7/18 16:46 | ||
| 10 | + */ | ||
| 11 | +class TencentCosService extends BaseService | ||
| 12 | +{ | ||
| 13 | + public $cos = [];//cos配置 | ||
| 14 | + | ||
| 15 | + public $method = '';//请求方式 | ||
| 16 | + | ||
| 17 | + public $time = 86400;//签名有效时间 | ||
| 18 | + | ||
| 19 | + public $config = [ | ||
| 20 | + 'cosRegion' => 'COS_REGION', // 存储桶地域 | ||
| 21 | + 'appId' => 'COS_APP_ID', // 腾讯云应用ID | ||
| 22 | + 'secretId' => 'COS_SECRET_ID', // 腾讯云API的SecretId | ||
| 23 | + 'secretKey' => 'COS_SECRET_KEY', // 腾讯云API的SecretKey | ||
| 24 | + 'bucket' => 'COS_BUCKET', // 存储桶名称 | ||
| 25 | + ]; | ||
| 26 | + | ||
| 27 | + public function __construct(){ | ||
| 28 | + $this->cos = config('filesystems.disks.cos'); | ||
| 29 | + $this->config['cosRegion'] = $this->cos['region']; | ||
| 30 | + $this->config['appId'] = $this->cos['credentials']['appId']; | ||
| 31 | + $this->config['secretId'] = $this->cos['credentials']['secretId']; | ||
| 32 | + $this->config['secretKey'] = $this->cos['credentials']['secretKey']; | ||
| 33 | + $this->config['bucket'] = $this->cos['bucket']; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * @remark :上传图片 | ||
| 38 | + * @name :upload | ||
| 39 | + * @author :lyh | ||
| 40 | + * @method :post | ||
| 41 | + * @time :2023/7/18 16:56 | ||
| 42 | + */ | ||
| 43 | + public function upload_image($path,$image_type){ | ||
| 44 | + // 构造请求方法、URL和头部 | ||
| 45 | + $url = 'https://' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'.$path; | ||
| 46 | + $this->method = 'PUT'; | ||
| 47 | + $headers = array( | ||
| 48 | + 'Authorization: ' . $this->cosAuthorization($this->config['appId'], $this->config['secretId'], $this->config['secretKey'], $this->method, '/' . $this->config['bucket'] . $path), | ||
| 49 | + 'Content-Type: image/'.$image_type // 根据上传的文件类型设置相应的Content-Type | ||
| 50 | + ); | ||
| 51 | + // 打开文件流 | ||
| 52 | + $url_path = config('filesystems.disks.upload')['root'].$path; | ||
| 53 | + $file = fopen($url_path, 'r'); | ||
| 54 | + return $this->http_put($url,$headers,$file,$url_path); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * @remark :上传文件到第三方 | ||
| 59 | + * @name : (http_put | ||
| 60 | + * @author :lyh | ||
| 61 | + * @method :post | ||
| 62 | + * @time :2023/7/18 17:06 | ||
| 63 | + */ | ||
| 64 | + public function http_put($url,$headers,$file,$url_path){ | ||
| 65 | + // 创建CURL句柄 | ||
| 66 | + $curl = curl_init(); | ||
| 67 | + // 设置请求方法、URL、头部和数据流 | ||
| 68 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); | ||
| 69 | + curl_setopt($curl, CURLOPT_URL, $url); | ||
| 70 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | ||
| 71 | + curl_setopt($curl, CURLOPT_INFILE, $file); | ||
| 72 | + curl_setopt($curl, CURLOPT_INFILESIZE, filesize($url_path)); | ||
| 73 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
| 74 | + // 发起请求 | ||
| 75 | + $response = curl_exec($curl); | ||
| 76 | + // 关闭文件流和CURL句柄 | ||
| 77 | + fclose($file); | ||
| 78 | + curl_close($curl); | ||
| 79 | + return $response; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * @remark : 构造腾讯云 COS 授权签名 | ||
| 84 | + * @name :cosAuthorization | ||
| 85 | + * @author :lyh | ||
| 86 | + * @method :post | ||
| 87 | + * @time :2023/7/18 17:10 | ||
| 88 | + */ | ||
| 89 | + function cosAuthorization($appId, $secretId, $secretKey, $method, $path) | ||
| 90 | + { | ||
| 91 | + $expired = time() + $this->time; // 签名有效期:当前时间戳 + 86400秒(1天) | ||
| 92 | + $toSign = 'a=' . $appId . '&b=' . $secretId . '&k=' . $secretKey . '&e=' . $expired . '&t=' . time() . '&r=' . rand() . '&f=' . $path; | ||
| 93 | + $sign = base64_encode(hash_hmac('SHA1', $toSign, $secretKey, true) . $toSign); | ||
| 94 | + return $sign; | ||
| 95 | + } | ||
| 96 | +} |
-
请 注册 或 登录 后发表评论