微信小程序/網站 上傳圖片到騰訊雲COS
阿新 • • 發佈:2018-05-15
arr n) ram index.php 函數返回 tps etag 循環 5.0
COS簡介: 騰訊雲提供的一種對象存儲服務,供開發者存儲海量文件的分布式存儲服務。可以將自己開發的應用的存儲部分全部接入COS的存儲桶中,有效減少應用服務器的帶寬,請求等。個人也可以通過騰訊雲賬號免費使用COS6個月,https://cloud.tencent.com/product/cos
整體流程簡介:
1. 前端引入cos的SDK文件
2. 監聽上傳控件,並在圖片加載至網頁臨時流中發起簽名請求
3.後端根據上傳的請求方式和路徑參數返回簽名Authorization和token XCosSecurityToken
4.前端再根據返回的參數和SDK以3的請求方式上傳圖片。
PHP簽名返回流程:
1.在騰訊雲的建好存儲桶並配置CORS規則https://cloud.tencent.com/document/product/436/11459
2.在平臺上拿到Bucket(存儲桶),Region(地域),SecretId,SecretKey等參數。
3.使用(SecretId,Timestamp…)參數進行簽名通過騰訊雲的接口獲取臨時密鑰,返回給前端的token也在臨時密鑰中
4.根據前端傳的(上傳請求方式,路徑)和臨時密鑰進行簽名(前端上傳所使用的)並返回。
一、PHP獲取簽名部分(tp5)
<?php
// +----------------------------------------------------------------------
// | When work is a pleasure, life is a joy!
// +----------------------------------------------------------------------
// | User: 傅超| Email:[email protected] | Time:2018/04/21 17:55
// +----------------------------------------------------------------------
// | TITLE: 小程序接口
// +----------------------------------------------------------------------
namespace app\v1\controller;
use think\Request;
use think\Db;
use app\v1\location\Location;
use think\Cache;
use \app\v1\auth\AccessToken;
use \app\v1\extend\Loginlog;
// 返回數據給前端
header(‘Content-Type: application/json‘);
header(‘Allow-Control-Allow-Origin: *‘); // 這裏修改允許跨域訪問的網站
// header(‘Allow-Control-Allow-Origin: http://127.0.0.1‘); // 這裏修改允許跨域訪問的網站
//header(‘Allow-Control-Allow-Origin: http://mer.runmoneyin.com‘); // 這裏修改允許跨域訪問的網站
header(‘Allow-Control-Allow-Headers: origin,accept,content-type‘);
/**
* Class Cosauth
* @title 獲取騰訊雲cos簽名接口
* @url http://119.29.10.64/v1/Cosauth
* @desc 小程序接口包含:獲取上傳圖片簽名
* @version 1.0
*/
class Cosauth extends Base
{
// 附加方法
protected $extraActionList = [‘getCosAuth‘, ‘getCosAuth‘];
// 跳過驗證方法
protected $skipAuthActionList = [‘getCosAuth‘, ‘getCosAuthEsay‘];
// appid
//protected $appid = ‘wx4c0e1852239664b4‘;
// cos配置參數
protected $config = array(
‘Url‘ => ‘https://sts.api.qcloud.com/v2/index.php‘,
‘Domain‘ => ‘sts.api.qcloud.com‘,
‘Proxy‘ => ‘‘,
‘SecretId‘ => ‘AK********************BLK9nF5dZL‘, // 固定密鑰
‘SecretKey‘ => ‘jHj5G*********************IUcqJu‘, // 固定密鑰
‘Bucket‘ => ‘activity-1255484416‘, // 存儲桶
‘Region‘ => ‘ap-guangzhou‘,
‘AllowPrefix‘ => ‘*‘, // 這裏改成允許的路徑前綴,這裏可以根據自己網站的用戶登錄態判斷允許上傳的目錄,例子:* 或者 a/* 或者 a.jpg
);
/**
* @title 獲取簽名入口
* http://119.29.10.64/v1/Cosauth/getCosAuth
*/
public function getCosAuth() {
// $data[‘say‘] = ‘hello‘;
// echo json_encode($data);
// die;
// 緩存臨時密鑰
if (!isset($_SESSION[‘tempKeysCache‘])) {
$_SESSION[‘tempKeysCache‘] = array(
‘policyStr‘ => ‘‘,
‘expiredTime‘ => 0
);
}
// 獲取前端過來的參數
// $method = isset($_GET[‘method‘]) ? $_GET[‘method‘] : ‘get‘;
// $pathname = isset($_GET[‘pathname‘]) ? $_GET[‘pathname‘] : ‘/‘;
$method = input(‘method‘) ? input(‘method‘) : ‘post‘;
$pathname = input(‘pathname‘) ? input(‘pathname‘) : ‘/‘;
$callback = input(‘callback‘) ? input(‘callback‘) : ‘‘; // 前端跨域的jsonp參數(可忽略)
// 獲取臨時密鑰,計算簽名
$tempKeys = $this->getTempKeys();
if ($tempKeys && $tempKeys[‘credentials‘]) {
// $datas = $this->getAuthorization($tempKeys, $method, $pathname);
// echo json_encode($datas);
// die;
$data = array(
‘Authorization‘ => $this->getAuthorization($tempKeys, $method, $pathname),
‘XCosSecurityToken‘ => $tempKeys[‘credentials‘][‘sessionToken‘],
);
} else {
$data = array(‘error‘=> $tempKeys);
}
//echo $callback . ‘(‘ . json_encode($data) . ‘)‘; // 通過回調返回給其他域(可忽略)
echo json_encode($data); // 正常寫法的返回
die;
}
// json 轉 query string
public function json2str($obj, $notEncode = false) {
ksort($obj);
$arr = array();
foreach ($obj as $key => $val) {
!$notEncode && ($val = urlencode($val));
array_push($arr, $key . ‘=‘ . $val);
}
return join(‘&‘, $arr);
}
// 計算臨時密鑰用的簽名
public function getSignature($opt, $key, $method) {
//global $config;
$formatString = $method . $this->config[‘Domain‘] . ‘/v2/index.php?‘ . $this->json2str($opt, 1);
$sign = hash_hmac(‘sha1‘, $formatString, $key);
$sign = base64_encode(hex2bin($sign));
return $sign;
}
// 獲取臨時密鑰
public function getTempKeys() {
//global $config;
// 判斷是否修改了 AllowPrefix
if ($this->config[‘AllowPrefix‘] === ‘_ALLOW_DIR_/*‘) {
return array(‘error‘=> ‘請修改 AllowPrefix 配置項,指定允許上傳的路徑前綴‘);
}
$ShortBucketName = substr($this->config[‘Bucket‘],0, strripos(