在laravel中使用OSS上传回调 WEB直传

控制器

/**
 * 阿里OSS后台签名并进行上传回调
 * Class UploadController
 * @package App\Http\Controllers
 */
class UploadController extends Controller
{
    /**
     * 返回OSS的签名验证
     * @return JSON 签名信息
     */
    public function getGetkey(Request $request)
    {
        //初始化一下必要的请求数据
        $id = env('ACCESSKEYID');   //AccessKeyId
        $key = env('ACCESSKEYSECRET');  //AccessKeySecret
        $host = 'http://outside-leader.oss-cn-qingdao.aliyuncs.com';  //OSS库地址
        $callbackUrl = url('upload/callback');  //上传回调的地址

        //上传回调的参数,callbackUrl地址,callbackBody回调接收的参数,callbackBodyType通过POST调用的回调函数,所以要设置这个头
        $callback_param = array(
            'callbackUrl' => $callbackUrl,
            'callbackBody' => 'filename=${object}&size=${size}&mimeType =${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}',
            'callbackBodyType' => "application/x-www-form-urlencoded"
        );
        $callback_string = json_encode($callback_param);  //转换成json格式
        $base64_callback_body = base64_encode($callback_string); //要返回的回调函数参数信息

        //设置过期时间
        $now = time();
        $expire = 30; //设置该policy超时时间是30s. 即这个policy过了这个有效时间,将不能访问
        $end = $now + $expire;
        $expiration = $this->gmt_iso8601($end);  //进行时间格式的转换

        $dir = $request['dir'];  //上传目录设置

        //处理上传限制条件
        //最大文件大小.用户可以自己设置
        $condition = array(0 => 'content-length-range', 1 => 0, 2 => 1048576000);
        $conditions[] = $condition; //设定文件大小
        //表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
        $start = array(0 => 'starts-with', 1 => '$key', 2 => $dir);
        $conditions[] = $start;  //必须以设定的目录开头,防止上传错误
        $arr = array('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, $key, true));  //要返回的签名信息

        //设置返回信息
        $response = array(
            'accessid' => $id,  //accessid
            'host' => $host,    //上传地址
            'policy' => $base64_policy,  //上传文件限制
            'signature' => $signature,   //签名信息
            'expire' => $end,    //失效时间
            'callback' => $base64_callback_body,  //上传回调参数
            'dir' => $dir     //上传的目录
        );
        return response()->json($response);   //返回信息

    }

    //格式化时间,格式为2016-07-07T23:48:43Z
    function gmt_iso8601($time)
    {
        $dtStr = date("c", $time);
        $pos = strpos($dtStr, '+');
        $expiration = substr($dtStr, 0, $pos);
        return $expiration . "Z";
    }

    //测试用的回调函数
    public function postCallback(Request $request)
    {
        // 1.获取OSS的签名header和公钥url header
        $authorizationBase64 = "";
        $pubKeyUrlBase64 = "";
        if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
            $authorizationBase64 = $_SERVER['HTTP_AUTHORIZATION'];
        }
        if (isset($_SERVER['HTTP_X_OSS_PUB_KEY_URL'])) {
            $pubKeyUrlBase64 = $_SERVER['HTTP_X_OSS_PUB_KEY_URL'];
        }
        if ($authorizationBase64 == '' || $pubKeyUrlBase64 == '') {
            header("http/1.1 403 Forbidden");
            exit();
        }

        // 2.获取OSS的签名
        $authorization = base64_decode($authorizationBase64);  //OSS签名

        // 3.获取公钥
        $pubKeyUrl = base64_decode($pubKeyUrlBase64);  //公钥的URL
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $pubKeyUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $pubKey = curl_exec($ch);
        if ($pubKey == "") {
            exit();   //header("http/1.1 403 Forbidden");
        }

        // 4.获取回调body,上传的图片的相关信息都在这里
        $body = file_get_contents('php://input');

        // 5.拼接待签名字符串
        $authStr = '';
        $path = $_SERVER['REQUEST_URI'];
        $pos = strpos($path, '?');
        if ($pos === false) {
            $authStr = urldecode($path) . "\n" . $body;
        } else {
            $authStr = urldecode(substr($path, 0, $pos)) . substr($path, $pos, strlen($path) - $pos) . "\n" . $body;
        }

        // 6.验证签名
        $ok = openssl_verify($authStr, $authorization, $pubKey, OPENSSL_ALGO_MD5);
        if ($ok == 1) {
            header("Content-Type: application/json");
            $data = array("Status" => "Ok");
            return response()->json($data);
        } else {
            exit(); //header("http/1.1 403 Forbidden");
        }
    }
}

前台html




    阿里OSS
    



上传文件名字保持本地文件名字 上传文件名字是随机文件名, 后缀保留

您所选择的文件列表:

你的浏览器不支持flash,Silverlight或者HTML5!



 

/

你可能感兴趣的:(在laravel中使用OSS上传回调 WEB直传)