http访问服务器资源对象,实现签名验证访问,使用腾讯云对象存储(Cloud Object Storage,COS)时,可通过 RESTful API 对 COS 发起 HTTP 匿名请求或 HTTP 签名请求,对于签名请求,COS 服务器端将会进行对请求发起者的身份验证

腾讯云COS签名访问

简介

使用腾讯云对象存储(Cloud Object Storage,COS)时,可通过 RESTful API 对 COS 发起 HTTP 匿名请求或 HTTP 签名请求,对于签名请求,COS 服务器端将会进行对请求发起者的身份验证。

匿名请求:HTTP 请求不携带任何身份标识和鉴权信息,通过 RESTful API 进行 HTTP 请求操作。
签名请求:HTTP 请求时携带签名,COS 服务器端收到消息后,进行身份验证,验证成功则可接受并执行请求,否则将会返回错误信息并丢弃此请求。
COS 基于密钥 HMAC(Hash Message Authentication Code)的自定义方案进行身份验证。

用处

公读访问

  1. 普通访问对象URL:https://www.nstns.com/1.zip
  2. 这种为公读可直接访问对象,可以机器+恶意访问造成+流量增大

私读访问

  1. 私读访问对象URL:https://www.nstns.com/1.zip?q-signature=7e42d4f96f6b3
  2. 携带签名请求,通过访问者唯一 ID 和密钥确定请求者身份,进行身份验证,验证成功则可接受并执行请求
  3. 通过特定KEY,动态时间戳,访问过期时间,计算出HMAC+HASH=签名值

签名步骤

签名HTTP 请求所需参数

根据 SecretId、KeyTime、HeaderList、UrlParamList 和 Signature 生成签名,格式为:

q-sign-algorithm=sha1
&q-ak=SecretId
&q-sign-time=KeyTime
&q-key-time=KeyTime
&q-header-list=HeaderList
&q-url-param-list=UrlParamList
&q-signature=Signature


步骤1:生成 KeyTime

通过KeyTime,设置文件可访问时间,过期时间
获取当前时间对应的 Unix 时间戳 StartTimestamp,Unix 时间戳是从 UTC(协调世界时,或 GMT 格林威治时间)1970年1月1日0时0分0秒(北京时间 1970年1月1日8时0分0秒)起至现在的总秒数
拼接签名有效时间,格式为StartTimestamp;EndTimestamp,即为 KeyTime。例如:1557902800;1557910000
PHP实现
time(); 直接返回系统 Unix 时间戳

$StartTimestamp = time();
print($StartTimestamp);

当前time返回时间戳+10分钟
实现访问时间,过期无法访问
strtotime();

$EndTimestamp = strtotime("+10 minute");// 加10分钟 10分钟后,URL过期
$KeyTime = "$StartTimestamp;$EndTimestamp";//拼凑KeyTime

步骤2:生成 SignKey

使用 HMAC-SHA1 以 SecretKey 为密钥,以 KeyTime 为消息,计算消息摘要(哈希值,16进制小写形式)
即为 SignKey,例如:eb2519b498b02ac213cb1f3d1a3d27a3b3c9bc5f

获取SecretKey:
腾讯云API>>:https://console.cloud.tencent.com/capi

202109011905.PNG

得到signKey
hash_hmac();

$SecretKey = "VKxxxxxxxxxxxxxxx";
#计算 signKey
$signKey = hash_hmac('sha1',$KeyTime,$SecretKey);

步骤3:生成 httpString

根据 HTTP 方法(HttpMethod)、HTTP 请求路径(UriPathname)、HttpParameters 和 HttpHeaders 生成 HttpString,格式为:

//生成格式为
HttpMethod\nUriPathname\nHttpParameters\nHttpHeaders\n
$httpMethod = 'get';//请求方法
$httpUri = '/x/url.txt';//请求路径文件
$httpParameters = '';//可为空
$headerString = '';//可为空
$httpString = "$httpMethod\n$httpUri\n$httpParameters\n$headerString\n";

步骤4:生成 StringToSign

根据 KeyTime 和 HttpString 生成 StringToSign
格式为 sha1\nKeyTime\nSHA1(HttpString)\n。
其中:
sha1 为固定字符串。
\n为换行符。
SHA1(HttpString) 为使用 SHA1 对 HttpString 计算的消息摘要,16进制小写形式,例如:54ecfe22f59d3514fdc764b87a32d8133ea611e6。

#计算 StringToSign
$sha1StringToSign = sha1($httpString);
$stringToSign = "sha1\n$KeyTime\n$sha1StringToSign\n";

步骤5:生成 signature

使用 HMAC-SHA1 以 SignKey 为密钥(字符串形式,非原始二进制),以 StringToSign 为消息,计算消息摘要,即为 Signature,例如:01681b8c9d798a678e43b685a9f1bba0f6c0e012

$signature = hash_hmac('sha1', $stringToSign, $signKey);

步骤6:生成签名

根据 SecretId、KeyTime、HeaderList、UrlParamList 和 Signature 生成签名
HTTP请求参数格式为

上述格式中的换行仅用于更好的阅读,实际格式并不包含换行。

q-sign-algorithm=sha1
&q-ak=SecretId
&q-sign-time=KeyTime
&q-key-time=KeyTime
&q-header-list=HeaderList
&q-url-param-list=UrlParamList
&q-signature=Signature
#拼凑 URl
$cos_url = $host.$httpUri."?q-sign-algorithm=sha1&q-ak=$SecretId&q-sign-time=$KeyTime&q-key-time=$KeyTime&q-header-list=&q-url-param-list=&q-signature=$signature";
print("访问链接:".$cos_url."\n");

最后完整PHP签名文件

简单实现,后续自改

<?php
 /* 
    * 腾讯云PHP计算COS签名
    * 2021-9-1
    * Xs小屋参考:https://www.nstns.com/cos-1.html
    * 官网参考:https://cloud.tencent.com/document/product/436/7778
    */

function get_cos(){

$SecretId =  "AKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//账号 SecretId
$SecretKey = "Vjxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//账号 SecretKey 控制台查看:https://console.cloud.tencent.com/capi
$host = "https://www.nstns.com";// 存储桶访问链接 cos控制台查看:https://console.cloud.tencent.com/cos5


#得到 KeyTime
$StartTimestamp = time();
$EndTimestamp = strtotime("+1 minute");// 加一分钟 一分钟后,URL过期
$KeyTime = "$StartTimestamp;$EndTimestamp";


#得到 signKey
$signKey = hash_hmac('sha1',$KeyTime,$SecretKey);


#得到 httpString
$httpMethod = 'get';//请求方法
$httpUri = '/x/x.txt';//请求路径文件
$httpParameters = '';//可为空
$headerString = '';//可为空
$httpString = "$httpMethod\n$httpUri\n$httpParameters\n$headerString\n";


#得到 StringToSign
$sha1StringToSign = sha1($httpString);
$stringToSign = "sha1\n$KeyTime\n$sha1StringToSign\n";


#得到 signature
$signature = hash_hmac('sha1', $stringToSign, $signKey);


#拼凑 URl
$cos_url = $host.$httpUri."?q-sign-algorithm=sha1&q-ak=$SecretId&q-sign-time=$KeyTime&q-key-time=$KeyTime&q-header-list=&q-url-param-list=&q-signature=$signature";
print("访问链接:".$cos_url."\n");
}

get_cos();
?>
最后修改:2021 年 09 月 01 日
如果觉得我的文章对你有用,请随意赞赏