作者 lyh

gx

... ... @@ -74,6 +74,7 @@ if (!function_exists('http_post')) {
}
}
if (!function_exists('http_get')) {
/**
* 发送http get请求
... ...
... ... @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Http\Controllers\type;
use App\Models\File\Image as ImageModel;
use App\Services\TencentCosService;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
... ... @@ -164,6 +165,7 @@ class ImageController extends Controller
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
return $this->uploadCos($this->path,$files->getClientOriginalExtension());
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
... ... @@ -317,4 +319,16 @@ class ImageController extends Controller
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
}
}
/**
* @remark :上传文件到cos
* @name :uploadCos
* @author :lyh
* @method :post
* @time :2023/7/18 17:38
*/
public function uploadCos($path,$image_type){
$txCos = new TencentCosService();
return $txCos->upload_image($path,$image_type);
}
}
... ...
<?php
namespace App\Services;
/**
* @remark :腾讯cos上传
* @class :TencentCosService.php
* @author :lyh
* @time :2023/7/18 16:46
*/
class TencentCosService extends BaseService
{
public $cos = [];//cos配置
public $method = '';//请求方式
public $time = 86400;//签名有效时间
public $config = [
'cosRegion' => 'COS_REGION', // 存储桶地域
'appId' => 'COS_APP_ID', // 腾讯云应用ID
'secretId' => 'COS_SECRET_ID', // 腾讯云API的SecretId
'secretKey' => 'COS_SECRET_KEY', // 腾讯云API的SecretKey
'bucket' => 'COS_BUCKET', // 存储桶名称
];
public function __construct(){
$this->cos = config('filesystems.disks.cos');
$this->config['cosRegion'] = $this->cos['region'];
$this->config['appId'] = $this->cos['credentials']['appId'];
$this->config['secretId'] = $this->cos['credentials']['secretId'];
$this->config['secretKey'] = $this->cos['credentials']['secretKey'];
$this->config['bucket'] = $this->cos['bucket'];
}
/**
* @remark :上传图片
* @name :upload
* @author :lyh
* @method :post
* @time :2023/7/18 16:56
*/
public function upload_image($path,$image_type){
// 构造请求方法、URL和头部
$url = 'https://' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'.$path;
$this->method = 'PUT';
$headers = array(
'Authorization: ' . $this->cosAuthorization($this->config['appId'], $this->config['secretId'], $this->config['secretKey'], $this->method, '/' . $this->config['bucket'] . $path),
'Content-Type: image/'.$image_type // 根据上传的文件类型设置相应的Content-Type
);
// 打开文件流
$url_path = config('filesystems.disks.upload')['root'].$path;
$file = fopen($url_path, 'r');
return $this->http_put($url,$headers,$file,$url_path);
}
/**
* @remark :上传文件到第三方
* @name : (http_put
* @author :lyh
* @method :post
* @time :2023/7/18 17:06
*/
public function http_put($url,$headers,$file,$url_path){
// 创建CURL句柄
$curl = curl_init();
// 设置请求方法、URL、头部和数据流
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_INFILE, $file);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($url_path));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 发起请求
$response = curl_exec($curl);
// 关闭文件流和CURL句柄
fclose($file);
curl_close($curl);
return $response;
}
/**
* @remark : 构造腾讯云 COS 授权签名
* @name :cosAuthorization
* @author :lyh
* @method :post
* @time :2023/7/18 17:10
*/
function cosAuthorization($appId, $secretId, $secretKey, $method, $path)
{
$expired = time() + $this->time; // 签名有效期:当前时间戳 + 86400秒(1天)
$toSign = 'a=' . $appId . '&b=' . $secretId . '&k=' . $secretKey . '&e=' . $expired . '&t=' . time() . '&r=' . rand() . '&f=' . $path;
$sign = base64_encode(hash_hmac('SHA1', $toSign, $secretKey, true) . $toSign);
return $sign;
}
}
... ...