PHP 之腾讯云对象存储生成临时地址

一、代码示例

/**
 * 生成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;
}

$secretKey:你的SecretKey

$accessKey:你的SecretId

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

二、签名工具

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

posted @ 2024-07-27 16:29  样子2018  阅读(4)  评论(0编辑  收藏  举报