Web端直传数据至OSS
最佳实践
- 小程序直传实践
- Web端PostObject直传实践
PHP生成 signature
Demo
<?php
class Alioss
{
protected $AccessKeySecret = 'saf2*******************sdkfenr';
/**
* get signature
*
* @return String
*/
public function getSignature()
{
$now = time();
$expire = 10; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
$end = $now + $expire;
$expiration = $this->gmt_iso8601($end);
$dir = '/uploads/'.date('Ym',time()).'/';
$conditions = [
['content-length-range', 0, 1048576000],
['starts-with', '$key', $dir],
];
$arr = [
'expiration'=>$expiration,
'conditions'=>$conditions,
];
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->$AccessKeySecret, true));
return $signature;
}
private function gmt_iso8601($time) {
$dtStr = date("c", $time);
$mydatetime = new \DateTime($dtStr);
$expiration = $mydatetime->format(\DateTime::ISO8601);
$pos = strpos($expiration, '+');
$expiration = substr($expiration, 0, $pos);
return $expiration."Z";
}
}
let the world have no hard-to-write code ^-^