Ucenter 头像存储到BOS插件
小豆吃竹子 发布于2016-06-21 17:29 浏览:2838 回复:1
2
收藏
2
收藏
1.Ucenter获取头像位置的程序是ucenter/avatar.php
2.上传头像的控制程序是ucenter/control/user.php : onrectavatar()这个函数。知道了这个之后,利用bos API签名算法就可以实现将ucenter头像直接上传到BOS。

主要是这个函数upload_bos:

    function upload_bos($localpath,$info,$path){
    // 内网和外网自行修改 推荐内网
    date_default_timezone_set('UTC');
    $timestamp = date("Y-m-d")."T".date("H:i:s")."Z";
    $host       = 'http://bj.bcebos.com';
    $AK  = '自己的AK';
    $SK = '自己的SK';
    $bucket     = 'flask-zxd';
    $expirationPeriodInSeconds = "3600";

    $authStringPrefix = "bce-auth-v1"."/".$AK."/".$timestamp."/".$expirationPeriodInSeconds;
    $SigningKey=hash_hmac('SHA256',$authStringPrefix,$SK);
    $CanonicalHeaders1 = "host;"."x-bce-date";
    $CanonicalHeaders2 = "host:bj.bcebos.com\n"."x-bce-date:".urlencode($timestamp);
    $CanonicalString = "";
    $CanonicalURI = '/' . $bucket . '/avatar/' . $path;
    $CanonicalRequest = "PUT\n".$CanonicalURI."\n".$CanonicalString."\n".$CanonicalHeaders2;    //第二步

    $Signature = hash_hmac('SHA256',$CanonicalRequest,$SigningKey);
    $Authorization = "bce-auth-v1/{$AK}/".$timestamp."/{$expirationPeriodInSeconds}/{$CanonicalHeaders1}/{$Signature}";
    print $Authorization."\n";

    $mime = $info['mime'];
    $head =  array("Content-Type: {$mime}","Authorization:{$Authorization}","x-bce-date:{$timestamp}");
    $timeout    = 30;
    $length     = filesize($localpath);
    $uri = '/' . $bucket . '/avatar/' . $path;

    $resource = fopen($localpath, 'r');
    $ch  = curl_init($host . $uri);

    curl_setopt($ch, CURLOPT_HEADER,true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_INFILE, $resource);
    curl_setopt($ch, CURLOPT_INFILESIZE, $length);
    curl_setopt ($ch, CURLOPT_UPLOAD, true );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    $response = curl_exec($ch);
    $status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    fclose($resource);
    if ($status == 200) {
        print "success";
        return true;
    } else {
        print $response;
        return false;
    }

签名算法可以参考百度的官方文档: https://bce.baidu.com/doc/Reference/AuthenticationMechanism.html

这两个文件我扔到github上了,地址: https://github.com/tanxiniao/bce/tree/master/ucenter_bos


收藏
点赞
2
个赞
共1条回复 最后由数字网络回复于2017-11-29 11:57
#2数字网络回复于2017-11-29

0
TOP