作者 lyh

gx

... ... @@ -40,22 +40,22 @@ class TencentCosService extends BaseService
* @method :post
* @time :2023/7/18 16:56
*/
public function upload_image($path,$image_type){
public function upload_image($path,$fileName,$image_type){
// 构建请求URL
$expiredTime = time() + 3600;
$pathname = '/'.$this->config['bucket'].'/'.$fileName;
$signature = $this->generateSignature($this->config['secretKey'], 'PUT', $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;
var_dump($url);
$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
);
var_dump($headers);
// 打开文件流
$url_path = config('filesystems.disks.upload')['root'].$path;
var_dump($url_path);
$file = fopen($url_path, 'r');
var_dump($file);
die();
return $this->http_put($url,$headers,$file,$url_path);
}
... ... @@ -97,11 +97,12 @@ class TencentCosService extends BaseService
* @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);
public function generateSignature($secretKey, $method, $pathname, $expiredTime) {
$signTime = time();
$plainText = "a=".$secretId."&k=".$secretKey."&e=".$expiredTime."&t=".$signTime."&r=".$pathname."&f=";
$bin = hash_hmac("SHA1", $plainText, $secretKey, true);
$bin = $bin . $plainText;
$sign = base64_encode($bin);
return $sign;
}
}
... ...