define('SecretId', 'SecretId');
define('SecretKey', 'SecretKey');
define('BucketURL', 'https://user-id.cos.ap-city.myqcloud.com');
main();
function main(){
cosUpload('01.txt', '/02.txt');
}
function cosUpload($file, $path) {
$url = BucketURL . $path;
$data = file_get_contents($file);
$header = array(
'Authorization: ' . RequestSign('put', $path),
'Date: ' . gmdate('D, d M Y H:i:s T')
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$data = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
return $data;
}
function RequestSign($method, $path) {
$signTime = (string)(time() - 60) . ';' . (string)(time() + 1200);
$host = parse_url(BucketURL);
$httpString = sprintf("%s\n%s\n\nhost=%s\n", strtolower($method), $path, $host['host']);
$stringToSign = sprintf("sha1\n%s\n%s\n", $signTime, sha1($httpString));
$signKey = hash_hmac('sha1', $signTime, SecretKey);
$signature = hash_hmac('sha1', $stringToSign, $signKey);
return sprintf('q-sign-algorithm=sha1&q-ak=%s&q-sign-time=%s&q-key-time=%s&q-header-list=host&q-url-param-list=&q-signature=%s', SecretId, $signTime, $signTime, $signature);
}