通过这个函数计算出来的url,可以直接下载对应的key,也就是你的文件
<?php
$signableQueryString = array(
'acl', 'delete', 'lifecycle', 'location', 'logging', 'notification',
'partNumber', 'policy', 'requestPayment', 'torrent', 'uploadId',
'uploads', 'versionId', 'versioning', 'versions', 'website',
'response-cache-control', 'response-content-disposition',
'response-content-encoding', 'response-content-language',
'response-content-type', 'response-expires', 'restore', 'tagging', 'cors'
);
function args_deal($request){
$query = "?";
foreach($request as $k=>$v){
$query.=$k;
if((strlen($v)!=0 && $v != "") || ($v=='0')){
$v = rawurlencode($v);
$query.="=".$v.'&';
}
else{
$query.='&';
}
}
return substr($query,0,-1);
}
$signableHeaders = array('Content-MD5', 'Content-Type');
function url(array $credentials){
$query = args_deal($credentials["query"]);
$sign = sign_create($credentials);
$object = rawurlencode($credentials['object']);
$url = "http://".$credentials['bucket'].".kss.ksyun.com/".$object.$query.'&Signature='.$sign;
echo $url;
}
function sign_create(array $credentials)
{
$stringToSign = createCanonicalizedString($credentials);
$sign = signString($stringToSign, $credentials["crendit"]["access_key"]);
return $sign;
}
function signString($string, $credentials)
{
return base64_encode(hash_hmac('sha1', $string, $credentials, true));
}
function createCanonicalizedString(array $req, $expires = null)
{
global $signableQueryString;
$buffer = $req['method'] . "\n\n\n";
$bucket = array_key_exists("bucket", $req) ? $req["bucket"]:null;
$buffer .= $bucket ? "/{$bucket}" : '';
$object = array_key_exists("object", $req) ? $req["object"]:null;
if ($object != null){
$object = rawurlencode($object);
}
$buffer .= $object ? "/{$object}" : '';
$query = $req["query"];
$first = true;
foreach ($signableQueryString as $key) {
if ( array_key_exists($key, $query) ) {
$value = $query[$key];
$buffer .= $first ? '?' : '&';
$first = false;
$buffer .= $key;
if ($value !== "") {
$buffer .= "={$value}";
}
}
}
return $buffer;
}
$listall = array(
'crendit' => array(
'access_id' => "your access id",
'access_key' => "your secret key",
),
'method' => 'GET',
'bucket' => 'your bucket',
'object' => 'your file',
'header' => array(),
'query' => array('response-content-disposition' =>'attachment; filename=fname.ext',
'response-content-type' => 'text/html'
)
);
echo url($listall);