作者 lyh

gx

... ... @@ -40,23 +40,16 @@ class TencentCosService extends BaseService
* @method :post
* @time :2023/7/18 16:56
*/
public function upload_image($path,$fileName,$image_type){
public function upload_image($path,$fileName){
// 构建请求URL
$expiredTime = time() + 3600;
$pathname = '/'.$this->config['bucket'].'/'.$fileName;
$signature = $this->generateSignature($this->config['secretKey'], 'PUT', $pathname, $expiredTime);
$signature = $this->generateSignature($this->config['secretKey'], $pathname, $expiredTime);
$url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname.'?sign='.$signature;
// 构造请求方法、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);
$fileContent = file_get_contents($url_path);
return $this->http_put($url,$fileContent);
}
/**
... ... @@ -66,27 +59,19 @@ class TencentCosService extends BaseService
* @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);
// 发起请求
curl_exec($curl);
// 关闭文件流和CURL句柄
fclose($file);
curl_close($curl);
// 检查响应状态码
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
return 1;
public function http_put($url,$fileContent){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContent);
$response = curl_exec($ch);
curl_close($ch);
// 检查上传结果
if ($response === false) {
echo '图片上传失败';
} else {
return 2;
echo '图片上传成功';
}
}
... ... @@ -97,9 +82,9 @@ class TencentCosService extends BaseService
* @method :post
* @time :2023/7/18 17:10
*/
public function generateSignature($secretKey, $method, $pathname, $expiredTime) {
public function generateSignature($secretKey, $pathname, $path ,$expiredTime) {
$signTime = time();
$plainText = "a=".$secretId."&k=".$secretKey."&e=".$expiredTime."&t=".$signTime."&r=".$pathname."&f=";
$plainText = "a=".$this->config['secretId']."&k=".$secretKey."&e=".$expiredTime."&t=".$signTime."&r=".$pathname."&f=".$path;
$bin = hash_hmac("SHA1", $plainText, $secretKey, true);
$bin = $bin . $plainText;
$sign = base64_encode($bin);
... ...