一、代码示例

/**
 * 生成cos签名
 * @param string $httpURI 你的url地址,如:https://xxxx.com/123/23/test.png
 * @param string $expires 有效时间 分钟
 * @param string $headerList
 * @param string $urlParamList
 * @param string $httpParameters
 * @param string $httpHeaders
 * @return string
 */
function createAuthorization($httpURI = '', $expires = '+5 minutes', $headerList = '', $urlParamList = '', $httpParameters = '', $httpHeaders = '')
{
    global $secretKey, $accessKey;
    $signTime = ( string )(time() - 60) . ';' . ( string )(strtotime($expires));
    $httpString = (string)"get\n" . $httpURI . "\n" . $httpParameters . "\n" . $httpHeaders . "\n";
    $sha1edHttpString = sha1($httpString);
    $stringToSign = (string)"sha1\n$signTime\n$sha1edHttpString\n";
    $signKey = hash_hmac('sha1', $signTime, $secretKey);
    $signature = hash_hmac('sha1', $stringToSign, $signKey);
    $signTime = urlencode($signTime);
    $query = "q-sign-algorithm=sha1&q-ak=" . trim($accessKey) . "&q-sign-time=$signTime&q-key-time=$signTime&q-header-list=$headerList&q-url-param-list=$urlParamList&" . "q-signature=$signature";
    return $query;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

$secretKey:你的SecretKey

$accessKey:你的SecretId

地址为:https://xxxx.com/123/23/test.png?你生成的签名

二、签名工具

 https://cos.cloud.tencent.com/tools/cos?tab=sign