一、为什么要用oss
将网站的静态资本存储在oss上,静态资本包孕网站图片,html,js,css文件,经由过程cdn将静态资本漫衍式缓存在各个节点上完成果就近拜候,提高用户拜候的照应速度.
二、oss如何用
1.在阿里云产品与效劳找到-工具存储oss
2.创立Bucket设置该读写权限(ACL)为 大众读
3.在AccessKey 打点创立AccessKey
三、创立上传节制器
class AliossController extends FwadminController {
private $oss_host = 'https://youboxunguanwang.oss-cn-shenzhen.aliyuncs.com';//'https://yifajian2020.oss-cn-beijing.aliyuncs.com';//阿里云oss 外网地点endpoint
private $oss_key_id = '';//阿里云oss Access Key ID
private $oss_key_secret = '';//阿里云oss Access Key Secret
private $oss_bucket_name = '';//创立的bucket称号
private $oss_endpoint = 'oss-cn-shenzhen.aliyuncs.com';//阿里云OSS外网地点
/*
* 获得署名
* @param string $path 保留路子
* @return json
*/
public function get_sign($path = '') {
$now = time();
$expire = 300000; //设置该policy超时时候是30s. 即这个policy过了这个有效时候,将不克不及拜候
$end = $now + $expire;
$expiration = $this->gmt_iso8601($end);
//最大文件巨细.用户能够自己设置
$condition = array(0=>'content-length-range', 1=>0, 2=>10485760000);
$conditions[] = $condition;
//流露显示用户上传的数据,必须是以$dir开始, 不然上传会失利,这一步不是必须项,只是为了平安起见,防止用户经由过程policy上传到别人的目次
$start = array(0=>'starts-with', 1=>$this->oss_key_secret, 2=>$path);
$conditions[] = $start;
$arr = array('expiration'=>$expiration,'conditions'=>$conditions);
//echo json_encode($arr);
//return;
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->oss_key_secret, true));
$response = array();
$response['accessid'] = $this->oss_key_id;
$response['host'] = $this->oss_host;
$response['policy'] = $base64_policy;
$response['signature'] = $signature;
$response['expire'] = $end;
//这个参数是设置用户上传指定的前缀
$response['dir'] = $path;
echo json_encode($response);
return ;
}
function gmt_iso8601($time) {
$dtStr = date("c", $time);
$mydatetime = new \DateTime($dtStr);
$expiration = $mydatetime->format(\DateTime::ISO8601);
$pos = strpos($expiration, '+');
$expiration2 = substr($expiration, 0, $pos);
return $expiration2."Z";
}
}
上传文件
/**
* 上传文件操作
*/
class AliossdController extends FwadminController {
/*
public $ossconfig = array(
public $ossconfig = array(
'id'=>'',//Access Key ID
'key'=>'', //Access Key Secret
'bucketname'=>'***x', //bucket称号
'host'=>'https://***x.oss-cn-beijing.aliyuncs.com', //上传提交地点 花式:bucketname+差别+阿里的域名
'expire' => 30, //过期时候
'callback_body' => array(
'callbackUrl'=>'', //回调地点全地点含有参数
'callbackHost'=>'', //回调域名
'callbackBody'=>'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}', //阿里前往的图片信息
'callbackBodyType'=>'application/x-www-form-urlencoded', //设置阿里前往的数据花式
),
'maxfilesize'=>10485760, //限制上传文件巨细 这里是10M
'imghost' =>'https://***x.oss-cn-beijing.aliyuncs.com', //前台显示图片的地点 花式不多说
);
public function _initialize() {
$this->ossconfig['host']= 'https://'.$this->ossconfig['bucketname'].'.oss-cn-beijing.aliyuncs.com'; //初始化上传地点
$this->ossconfig['callback_body']['callbackUrl']='https://'.$_SERVER['HTTP_HOST'].'/s****n.php/ossupload/cupload/'; //初始化回调地点
$this->ossconfig['callback_body']['callbackHost']=$_SERVER['HTTP_HOST']; //初始化回调域名
}
//获得policy和回调地点 一样平时应用jajx或是在加载页面的时候会用到policy和回调地点,