正在显示
2 个修改的文件
包含
0 行增加
和
84 行删除
| @@ -165,8 +165,6 @@ class ImageController extends Controller | @@ -165,8 +165,6 @@ class ImageController extends Controller | ||
| 165 | if ($rs === false) { | 165 | if ($rs === false) { |
| 166 | return $this->response('添加失败', Code::USER_ERROR); | 166 | return $this->response('添加失败', Code::USER_ERROR); |
| 167 | } | 167 | } |
| 168 | - $cos = new TencentCosService(); | ||
| 169 | - return $cos->uploadToCos($this->path,$fileName); | ||
| 170 | return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); | 168 | return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); |
| 171 | } | 169 | } |
| 172 | 170 |
app/Services/TencentCosService.php
已删除
100644 → 0
| 1 | -<?php | ||
| 2 | - | ||
| 3 | -namespace App\Services; | ||
| 4 | - | ||
| 5 | -use GuzzleHttp\Client; | ||
| 6 | -use Illuminate\Http\Request; | ||
| 7 | -use Illuminate\Support\Str; | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * @remark :腾讯cos上传 | ||
| 11 | - * @class :TencentCosService.php | ||
| 12 | - * @author :lyh | ||
| 13 | - * @time :2023/7/18 16:46 | ||
| 14 | - */ | ||
| 15 | -class TencentCosService extends BaseService | ||
| 16 | -{ | ||
| 17 | - public $cos = [];//cos配置 | ||
| 18 | - | ||
| 19 | - public $method = '';//请求方式 | ||
| 20 | - | ||
| 21 | - public $time = 86400;//签名有效时间 | ||
| 22 | - | ||
| 23 | - public $config = [ | ||
| 24 | - 'cosRegion' => 'COS_REGION', // 存储桶地域 | ||
| 25 | - 'appId' => 'COS_APP_ID', // 腾讯云应用ID | ||
| 26 | - 'secretId' => 'COS_SECRET_ID', // 腾讯云API的SecretId | ||
| 27 | - 'secretKey' => 'COS_SECRET_KEY', // 腾讯云API的SecretKey | ||
| 28 | - 'bucket' => 'COS_BUCKET', // 存储桶名称 | ||
| 29 | - ]; | ||
| 30 | - | ||
| 31 | - public function __construct(){ | ||
| 32 | - $this->cos = config('filesystems.disks.cos'); | ||
| 33 | - $this->config['cosRegion'] = $this->cos['region']; | ||
| 34 | - $this->config['appId'] = $this->cos['credentials']['appId']; | ||
| 35 | - $this->config['secretId'] = $this->cos['credentials']['secretId']; | ||
| 36 | - $this->config['secretKey'] = $this->cos['credentials']['secretKey']; | ||
| 37 | - $this->config['bucket'] = $this->cos['bucket']; | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | - /** | ||
| 41 | - * @remark :上传图片 | ||
| 42 | - * @name :uploadToCos | ||
| 43 | - * @author :lyh | ||
| 44 | - * @method :post | ||
| 45 | - * @time :2023/7/19 14:47 | ||
| 46 | - */ | ||
| 47 | - public function uploadToCos($path,$filename) | ||
| 48 | - { | ||
| 49 | - $objectKey = $path.'/'.$filename; | ||
| 50 | - // 上传到腾讯云 COS | ||
| 51 | - $httpClient = new Client(); | ||
| 52 | - $response = $httpClient->request('PUT', "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$objectKey}", [ | ||
| 53 | - 'body' => file_get_contents(config('filesystems.disks.upload')['root'].$objectKey), | ||
| 54 | - 'headers' => $this->generateHeaders($this->config['secretId'], $this->config['secretKey']), | ||
| 55 | - ]); | ||
| 56 | - // 若上传成功,则返回上传后的 URL | ||
| 57 | - var_dump($response); | ||
| 58 | - die(); | ||
| 59 | - if ($response->getStatusCode() === 200) { | ||
| 60 | - return "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$objectKey}"; | ||
| 61 | - } | ||
| 62 | - return null; | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - /** | ||
| 66 | - * @remark :签名 | ||
| 67 | - * @name :generateHeaders | ||
| 68 | - * @author :lyh | ||
| 69 | - * @method :post | ||
| 70 | - * @time :2023/7/19 14:47 | ||
| 71 | - */ | ||
| 72 | - private function generateHeaders($secretId, $secretKey) | ||
| 73 | - { | ||
| 74 | - $keyTime = time() - 1 . ';' . (time() + 300); | ||
| 75 | - $signKey = hash_hmac('sha1', $keyTime, $secretKey); | ||
| 76 | - $signature = hash_hmac('sha1', "sha1\n{$keyTime}\n\n", $signKey); | ||
| 77 | - return [ | ||
| 78 | - 'Authorization' => 'q-sign-algorithm=sha1&q-ak=' . $secretId . '&q-sign-time=' . $keyTime | ||
| 79 | - . '&q-key-time=' . $keyTime . '&q-signature=' . $signature, | ||
| 80 | - ]; | ||
| 81 | - } | ||
| 82 | -} |
-
请 注册 或 登录 后发表评论