作者 lyh

gx

... ... @@ -43,8 +43,16 @@ class TencentCosService extends BaseService
public function upload_image($path,$fileName){
// 构建请求URL
$pathname = '/'.$this->config['bucket'].$path.'/'.$fileName;
$signature = $this->generateSignature($path.'/'.$fileName);
$url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname.'?sign='.$signature;
$url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname;
$date = gmdate('D, d M Y H:i:s T');
$signKey = hash_hmac('sha1', $date, $this->config['secretKey']);
$method = 'PUT';
$signature = base64_encode(hash_hmac('sha1', "{$method}\n\n\n\n", $signKey, true));
$headers = array(
'Authorization: QCloud ' . $this->config['secretId'] . ':' . $signature,
'Date: ' . $date,
'Host: ' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'
);
// 打开文件流
$url_path = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;
$fileContent = file_get_contents($url_path);
... ... @@ -58,37 +66,25 @@ class TencentCosService extends BaseService
* @method :post
* @time :2023/7/18 17:06
*/
public function http_put($url,$fileContent){
public function http_put($url,$filePath,$headers){
// 执行请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContent);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'rb'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
// 检查上传结果
if ($response === false) {
echo '图片上传失败';
} else {
// 检查响应结果
if ($response) {
var_dump($response);
die();
echo '图片上传成功';
} else {
echo '图片上传失败';
}
}
/**
* @remark : 构造腾讯云 COS 授权签名
* @name :cosAuthorization
* @author :lyh
* @method :post
* @time :2023/7/18 17:10
*/
public function generateSignature($path) {
$signTime = time();
$expiredTime = 0;
$plainText =
'a='.$this->config['appId'].'&b='.$this->config['bucket']. '&k='.$this->config['secretId'].'&e='.$expiredTime.'&t='.$signTime.'&r='.rand().'&f='.$path;
$sign = base64_encode(hash_hmac('SHA1', $plainText, $this->config['secretKey'], true).$plainText);
return $sign;
}
}
... ...