Web端直傳數據至OSS


官方文檔

最佳實踐

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";
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM