php签名-腾讯云cos通过javascript上传文件,php生成签名

2023-08-27 0 452 百度已收录

准备:

1 准备好您的密钥和存储桶名称

登录腾讯云控台北,从对象存储-密钥管理-api密钥中获取固定密钥(secretId、secretKey)

,然后在对象存储-桶列表中创建桶,获取桶名称(bucket)和创建桶时选择的区域(region),如中国-上海“ap-shanghai”

2 构建签名生成API

我这边使用的是php

a) 下载 COS STS SDK 并获取 sts 的一个类

或者

<?php
namespace QCloudCOSSTS;
class Sts{
	// 临时密钥计算样例
	
	function _hex2bin($data) {
		$len = strlen($data);
		return pack("H" . $len, $data);
	}
	// obj 转 query string
	function json2str($obj, $notEncode = false) {
		ksort($obj);
		$arr = array();
		if(!is_array($obj)){
			throw new Exception('$obj must be an array, the actual value is:' . json_encode($obj));
		}
		foreach ($obj as $key => $val) {
			array_push($arr, $key . '=' . ($notEncode ? $val : rawurlencode($val)));
		}
		return join('&', $arr);
	}
	// 计算临时密钥用的签名
	function getSignature($opt, $key, $method, $config) {
		$formatString = $method . $config['domain'] . '/?' . $this->json2str($opt, 1);
		$sign = hash_hmac('sha1', $formatString, $key);
		$sign = base64_encode($this->_hex2bin($sign));
		return $sign;
	}
	// v2接口的key首字母小写,v3改成大写,此处做了向下兼容
	function backwardCompat($result) {
		if(!is_array($result)){
			throw new Exception('$result must be an array, the actual value is:' . json_encode($result));
		}
		$compat = array();
		foreach ($result as $key => $value) {
			if(is_array($value)) {
				$compat[lcfirst($key)] = $this->backwardCompat($value);
			} elseif ($key == 'Token') {
				$compat['sessionToken'] = $value;
			} else {
				$compat[lcfirst($key)] = $value;
			}
		}
		return $compat;
	}
	// 获取临时密钥
	function getTempKeys($config) {
				$result = null;
		try{
			if(array_key_exists('policy', $config)){
				$policy = $config['policy'];
			}else{
				if(array_key_exists('bucket', $config)){
					$ShortBucketName = substr($config['bucket'],0, strripos($config['bucket'], '-'));
					$AppId = substr($config['bucket'], 1 + strripos($config['bucket'], '-'));
				}else{
					throw new Exception("bucket== null");
				}
				if(array_key_exists('allowPrefix', $config)){
					if(!(strpos($config['allowPrefix'], '/') === 0)){
					$config['allowPrefix'] = '/' . $config['allowPrefix'];
					}
				}else{
					throw new Exception("allowPrefix == null");
				}
				if(!array_key_exists('region', $config)) {
					throw new Exception("region == null");
				}
				$policy = array(
					'version'=> '2.0',
					'statement'=> array(
						array(
							'action'=> $config['allowActions'],
							'effect'=> 'allow',
							'principal'=> array('qcs'=> array('*')),
							'resource'=> array(
								'qcs::cos:' . $config['region'] . ':uid/' . $AppId . ':' . $config['bucket'] . $config['allowPrefix']
							)
						)
					)
				);	
			}
			$policyStr = str_replace('\/', '/', json_encode($policy));
			$Action = 'GetFederationToken';
			$Nonce = rand(10000, 20000);
			$Timestamp = time();
			$Method = 'POST';
			if(array_key_exists('durationSeconds', $config)){
				if(!(is_integer($config['durationSeconds']))){
					throw new Exception("durationSeconds must be a int type");
				}
			}
			$params = array(
				'SecretId'=> $config['secretId'],
				'Timestamp'=> $Timestamp,
				'Nonce'=> $Nonce,
				'Action'=> $Action,
				'DurationSeconds'=> $config['durationSeconds'],
				'Version'=>'2018-08-13',
				'Name'=> 'cos',
				'Region'=> $config['region'],
				'Policy'=> urlencode($policyStr)
			);
			$params['Signature'] = $this->getSignature($params, $config['secretKey'], $Method, $config);
			$url = $config['url'];
			$ch = curl_init($url);
			if(array_key_exists('proxy', $config)){
				$config['proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
			}
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
			curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $this->json2str($params));
			$result = curl_exec($ch);
			if(curl_errno($ch)) $result = curl_error($ch);
			curl_close($ch);
			$result = json_decode($result, 1);
			if (isset($result['Response'])) {
				$result = $result['Response'];
				if(isset($result['Error'])){
					throw new Exception("get cam failed");
				}
				$result['startTime'] = $result['ExpiredTime'] - $config['durationSeconds'];
			}
			$result = $this->backwardCompat($result);
			return $result;
		}catch(Exception $e){
			if($result == null){
				$result = "error: " . $e->getMessage();
			}else{
				$result = json_encode($result);
			}
			throw new Exception($result);
		}
	}
	
	// get policy
	function getPolicy($scopes){
		if (!is_array($scopes)){
			return null;
		}
		$statements = array();
		
		for($i=0, $counts=count($scopes); $i < $counts; $i++){
			$actions=array();
			$resources = array();
			array_push($actions, $scopes[$i]->get_action());
			array_push($resources, $scopes[$i]->get_resource());
			
			$statement = array(
			'action' => $actions,
			'effect' => $scopes[$i]->get_effect(),
			'resource' => $resources
			);
			array_push($statements, $statement);
		}
			
		$policy = array(
			'version' => '2.0',
			'statement' => $statements
		);
		return $policy;
	}	
}
?>

b) 实例化计算签名(需要使用第一步得到的4个参数)

	  $sts = new STS();
      // 配置参数
      $config = array(
          'url' => 'https://sts.tencentcloudapi.com/',
          'domain' => 'sts.tencentcloudapi.com',
          'proxy' => '',
          'secretId' => $this->secretId, // secretId
          'secretKey' => $this->secretKey, // secretKey
          'bucket' => $this->bucket, //bucket
          'region' => $this->region, // region
          'durationSeconds' => 1800, // 密钥有效期
          'allowPrefix' => '*', // 允许的路径前缀,例子:* 或者 a/* 或者 a.jpg
          // 密钥的权限列表。
          'allowActions' => array (
              // 所有 action 请看文档 https://cloud.tencent.com/document/product/436/31923
              // 简单上传
              'name/cos:PutObject',
              'name/cos:PostObject',
              // 分片上传
              'name/cos:InitiateMultipartUpload',
              'name/cos:ListMultipartUploads',
              'name/cos:ListParts',
              'name/cos:UploadPart',
              'name/cos:CompleteMultipartUpload',
              'name/cos:DeleteObject'
          )
      );
      // 获取临时密钥,计算签名
      $tempKeys = $sts->getTempKeys($config);
      // 返回数据给前端
      header('Content-Type: application/json');
      header('Access-Control-Allow-Origin: http://127.0.0.1'); // 这里修改允许跨域访问的网站
      header('Access-Control-Allow-Headers: origin,accept,content-type');
      echo json_encode($tempKeys);

签名准备好后php签名php签名,javascript

1 页面中引入cos-js-sdk-v5.min.js

<script src="https://unpkg.com/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js"></script>

或者从以下位置下载最新版本

2 在页面中实例

	var cos = new COS({
         getAuthorization: function (options, callback) {
               // 异步获取临时密钥
               $.get(getUrl('准备工作中生成签名的api链接'), {
                   bucket: options.Bucket,
                   region: options.Region,
               }, function (data) {
                   var credentials = data.credentials;
                   callback({
                       TmpSecretId: credentials.tmpSecretId,
                       TmpSecretKey: credentials.tmpSecretKey,
                       XCosSecurityToken: credentials.sessionToken,
                       ExpiredTime: data.expiredTime
                   });
               });
           }
       })

3 简单的上传和下载

上传:

	cos.putObject({
	   Bucket: bucket, //准备工作中的 储存桶名称 bucket ,比如 'wpq-12345678'
	   Region: region, //准备工作中的 所在区域 region ,比如 'ap-shanghai'
	   Key: 'exampleobject', //上传成功后,在储存桶中的 文件路径 + 名字,比如 'zhangsan/aaa.txt'
	   Body: fileObject, // 上传文件对象,比如 input file中的对象,upload.files[0]
	   onProgress: function(progressData) {
	       console.log(JSON.stringify(progressData));
	   }
	}, function(err, data) {
	   console.log(err || data);
	});

下载:

	cos.getObject({
	   Bucket: bucket, //准备工作中的 储存桶名称 bucket ,比如 'wpq-12345678'
	   Region: region, //准备工作中的 所在区域 region ,比如 'ap-shanghai'
	   Key: 'exampleobject', //在储存桶中的 文件路径 + 名字,比如 'zhangsan/aaa.txt'
	}, function(err, data) {
	   console.log(err || data.Body);
	});

文章部分内容参考腾讯云点播官方文档

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 php php签名-腾讯云cos通过javascript上传文件,php生成签名 https://www.wkzy.net/game/166170.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务