web直接上传oss_Web端直传数据至OSS

客官°小女子只卖身不卖艺 2023-01-08 13:29 231阅读 0赞

最佳实践

PHP生成 signature Demo

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”;

}

}

发表评论

表情:
评论列表 (有 0 条评论,231人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PHP实现图片阿里云OSS

           今天博主在实现一个头像上传功能,需要将图片上传至阿里云的OSS,所以也是百度、谷歌了一番,但都不是很管用,所以自己研究了一番,并将上传的方法封装成一个类进行调用,