阿里云 oss 视频上传 sts+前端js分片上传

目录

第1步 必要参数获取

第2步 执行下面php后端请求接口代码的两个类进行sts获取令牌获取 

 2.1  php后端请求接口 

 2.2  返回结果

第3步  前端获通过js获取上面的返回的3个参数 实现分片上传

第4步 上传成功后异步回调处理


第1步 必要参数获取

参数名

参数说明

备注

Endpoint

 

 

Bucket域名

 

 

Bucketname

Bucket名称

 

用户名

子账号用户名

 

AccessKeyId

子账号keyid

-

AccessKeySecret

子账号秘钥

 

format 格式 json或xml  
version 2015-04-01 2015-04-01
signaturemethod HMAC-SHA1  
signatureversion 1.0  
action AssumeRole  
RoleArn    
Bucketurl    

 

*下面是我的配置信息可以参考(标红的需要更改自己的OSS配置)

[
    'bucket' => 'bucketnametest',
    'endpoint' => 'oss-cn-shanghai.aliyuncs.com',
    'format' => 'json',
    'version' => '2015-04-01',
    'accesskeyid' => 'LTAIZy0T6hrePIVe',
    'ck' => 'CJWJfF9YtmC5tKoKEH6kh5tjMO2Lrh',
    'signaturemethod' => 'HMAC-SHA1',
    'signatureversion' => '1.0',
    'action' => 'AssumeRole',
    'RoleArn' => 'acs:ram::1609196445907631:role/aliyunosstokengenrratorrole',
    'Bucketurl'=> 'beh.oss-cn-shanghai.aliyuncs.com'
]

*在走以下步骤之前先在你的阿里云oss中获取以上的参数信息因为下面步骤要用到

阿里云oss配置说明可参考:https://blog.csdn.net/bianenhui91/article/details/83276193

 

 

 


第2步 执行下面php后端请求接口代码的两个类进行sts获取令牌 

获取 AccessKeySecret 、AccessKeyId 、SecurityToken 这3个参数(sts)返回的结果(2.2小节)

  

    2.1  php后端请求接口 

*下面php代码直接复制到自己的服务器下是可以执行返回数据,需要把刚开始获取的配置参数bucket、endpoint、format、version、accesskeyid、ck、signaturemethod 、signatureversion 、action、RoleArn下面填上即可,有两处需要加上这些参数

 self::format,
            'RoleArn'=>self::RoleArn,
            'RoleSessionName'=>$RoleSessionName,
            'Action' => self::action,
            'Version' => self::version,
            'SignatureMethod' => self::signaturemethod,
            'SignatureNonce' => time(),
            'SignatureVersion' => self::signatureversion,
            'AccessKeyId' =>self::accesskeyid,
            'Timestamp' => date('Y-m-d',$h_8_time).'T'.date('H:i:s',$h_8_time).'Z'
        );
        return $get_key_value;
    }
}


/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/7
 * Time: 13:12
 */
class sysshop_sts_getToken{

    //以下参数需要从自己的oss获取相对应的参数
    CONST bucket = '';
    CONST endpoint = '';
    CONST format = '';
    CONST version = '';
    CONST accesskeyid = '';

    //ck是秘钥
    CONST ck = '';
    CONST signaturemethod = '';
    CONST signatureversion = '';
    CONST action = '';
    CONST RoleArn = '';
    private $format;
    private $version;
    private $accesskeyid;
    private $signature;
    private $signaturemethod;
    private $signatureversion;
    private $signaturenonce;
    private $timestamp;
    private $action;
    private $RoleArn;
    private $RoleSessionName;
    public function getformat()
    {
        return $this->format;
    }
    public function getversion()
    {
        return $this->version;
    }
    public function getaccesskeyid()
    {
        return $this->accesskeyid;
    }
    public function getsignature()
    {
        return $this->signature;
    }
    public function getsignaturemethod(){
        return $this->signaturemethod;
    }
    public function getsignatureversion(){
        return $this->signatureversion;
    }
    public function getsignaturenonce(){
        return $this->signaturenonce;
    }
    public function gettimestamp(){
        return $this->timestamp;
    }
    public function getaction(){
        return $this->action;
    }

    /**
     * @return string
     */
    public function getRoleArn()
    {
        return $this->RoleArn;
    }

    /**
     * @return string
     */
    public function getRoleSessionName()
    {
        return $this->RoleSessionName;
    }

    public function index($sessionname)
    {
        
        $sysshop_sts_util = new sysshop_sts_util();
        $this->RoleSessionName = $sessionname;
        $h_8_time = time()-8*60*60;
        $get_key = $sysshop_sts_util->getkey();
        $get_key_value = $sysshop_sts_util->getkeyvalue($this->RoleSessionName);
        $StringToSign = $sysshop_sts_util->getsignature($get_key,$get_key_value);
        $this->format = self::format;
        $this->version = self::version;
        $this->accesskeyid = self::accesskeyid;
        $ck = self::ck;
        $this->signature = base64_encode(hash_hmac("sha1",$StringToSign,$ck.'&',true));
        $this->signaturemethod = self::signaturemethod;
        $this->signatureversion = self::signatureversion;
        $this->signaturenonce = time();
        $this->timestamp = date('Y-m-d',$h_8_time).'T'.date('H:i:s',$h_8_time).'Z';
        $this->action = self::action;
        $this->RoleArn = self::RoleArn;
        return $sysshop_sts_util->request_curl($this->formatpostdata());

    }

    /**
     * 格式化参数
     * @return mixed
     */
    public function formatpostdata(){
        $data['Format'] = $this->getformat();
        $data['Version'] = $this->getversion();
        $data['AccessKeyId'] = $this->getaccesskeyid();
        $data['Signature'] = $this->getsignature();
        $data['SignatureMethod'] = $this->getsignaturemethod();
        $data['SignatureVersion'] = $this->getsignatureversion();
        $data['SignatureNonce'] = $this->getsignaturenonce();
        $data['Timestamp'] = $this->gettimestamp();
        $data['Action'] = $this->getaction();
        $data['RoleArn'] = $this->getRoleArn();
        $data['RoleSessionName'] = $this->getRoleSessionName();
        return $data;
    }
}
$sysshop_sts_getToken = new sysshop_sts_getToken();
$sessionname = 'test';
$callback = $sysshop_sts_getToken->index($sessionname);
print_r($callback);

 2.2  返回结果

         {
            "RequestId": "0548D3A8-CE25-454A-AD99-02B758CF992D",
            "AssumedRoleUser": {
                "AssumedRoleId": "325499844018321549:test",
                "Arn": "acs:ram::1609196445907631:role/aliyunosstokengeneratorrole/test"
                },
            "Credentials": {
                "
AccessKeySecret": "6jLdHbeUiiLk29eonTwLLiKEgJu8Wpbq26m58vYrHoPe",
                "
AccessKeyId": "STS.NK84yMKu6WW7va1WyMcvfJxCZ",
                "Expiration": "2018-10-17T03:17:31Z",
                "
SecurityToken":  "CAIS/gF1q6Ft5B2yfSjIr4iNf8P5pqoX4JXcdEeAs3kYb/lKpb3ouDz2IHpJeHhhCewatv0/n2pR6/oTlq1vRoRZHe8qmCPwqsY5yxioRqacke7XhOV2pf/IMGyXDAGBr622Su7lTdTbV+6wYlTf7EFayqf7cjPQND7Mc+f+6/hdY88QQxOzYBdfGd5SPXECksIBMmbLPvvfWXyDwEioVRQw61Mt1z8jtPXnnZPHsyCz1gOqlrUnwK3qOYWhYsVWO5Nybsy4xuQedNCainMMukkaqPsu1fwfo26a4oCHcFBV4gSbNe3P6cFoLwJ/aaU8FrRNsP/mj/p8t/wUCkxrQI/IVxqAAbEkcJnat7qTMuyWDAtaSbzSC/yhh40LNE0ddVomBe464G2+92WZtPsSBNT33tNlHItBa9FV6wudlwwDYcmD3b4V3Le9GVDsIQSBor3mpI5R+Rw4BKxZqkM9PacLpC5IuYJtai6vdVdcDaWcyyl6lCxqKnMHPrh1RJqemA+Is9FQ"           
               }
         }

 


第3步  前端获通过js获取上面的返回的3个参数 实现分片上传

*实际开发中遇到一个问题,在上传文件的时候出现了aliyun-oss-sdk-4.4.4.min.js这个文件js某些方法报错,之后修改了前端ajax请求方式例如jquery后端获取令牌接口的方式然后问题解决了,这一步也需要用到前面获取的参数,对应填好即可



   


  
  

 

 


第4步 上传成功后异步回调处理

true,
                'callbackdata'=>$_POST
            );
            $callbackinfo = $_POST;
            //网站业务处理
            echo json_encode($data);
        }
        else
        {
            exit();
        }
    }
}

你可能感兴趣的:(php)