| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Model_aliyunossnew extends Lin_Model
- {
- function __construct(){
- parent::__construct();
- }
- private $oss_basic_url = "https://lyerp-product-source.oss-cn-beijing.aliyuncs.com";
- private $show_basie_url ="https://lyerposs.wepolicy.cn";
- private function get_oss_config(){
- $query1 = $this->db->get_where('setting',array('skey' => 'oss_aliyun_id'));
- $oss_aliyun_id_info = $query1->row_array();
- $query2 = $this->db->get_where('setting',array('skey' => 'oss_aliyun_key'));
- $oss_aliyun_key_info = $query2->row_array();
- $accessKeyId = $oss_aliyun_id_info['svalue'];
- $accessKeySecret = $oss_aliyun_key_info['svalue'];
- $bucket = "lyerp-product-source";
- $endpoint = "oss-cn-beijing.aliyuncs.com";
- $region = "oss-cn-beijing";
- $host = $bucket.".".$endpoint;
- return [
- 'accessKeyId' => $accessKeyId,
- 'accessKeySecret' => $accessKeySecret,
- 'bucket' => $bucket,
- 'endpoint' => $endpoint,
- 'region' => $region,
- 'host' => $host,
- ];
- }
- public function getOssSign(){
- $conf = $this->get_oss_config();
- $accessKeyId = $conf['accessKeyId'];
- $accessKeySecret = $conf['accessKeySecret'];
- $region = $conf['region'];
- $bucket = $conf['bucket'];
- $endpoint = $conf['endpoint'];
- $host = $conf['host'];
- // 1. 获取OSS的签名信息
- $date_folder = date('Ymd');
- $dir = "uploads/{$date_folder}/";
- //1 .设置上传子目录(按日期分类)
- $expiration = date('Y-m-d\TH:i:s\Z', time() + 300); // 5分钟后过期
- $conditions = [
- ["content-length-range", 0, 1048576000], // 限制文件大小,这里设置为最大1GB
- ["starts-with", '$key', $dir] // 限制文件上传的目录前缀
- ];
- $policy = json_encode(['expiration' => $expiration, 'conditions' => $conditions]);
- $base64Policy = base64_encode($policy);
- // 2. 计算签名
- $signature = base64_encode(hash_hmac('sha1', $base64Policy, $accessKeySecret, true));
- // 3. 返回给前端的数据
- $response = [
- 'accessKeyId' => $accessKeyId,
- 'accessKeySecret'=>$accessKeySecret,
- 'bucket' => $bucket,
- 'region' => $region,
- 'dir' => $dir,
- 'policy' => $base64Policy,
- 'signature' => $signature,
- 'host' => $host,
- 'expire' => time() + 3600,
- 'oss_url'=>$this->oss_basic_url,
- 'show_url'=>$this->show_basie_url
- ];
- return [
- 'code'=>1,
- 'msg'=>'success',
- 'data'=>$response
- ];
- }
- //对于存到阿里云的oss 进行类别区分
- public function getOssSignType($type){
- if(empty($type)){
- return [
- "code"=>-1,
- "msg"=>"存储类别不对",
- "data"=>[]
- ];
- }
- $conf = $this->get_oss_config();
- $accessKeyId = $conf['accessKeyId'];
- $accessKeySecret = $conf['accessKeySecret'];
- $region = $conf['region'];
- $bucket = $conf['bucket'];
- $endpoint = $conf['endpoint'];
- $host = $conf['host'];
- // 1. 获取OSS的签名信息
- $date_folder = date('Ymd');
- $dir = "{$type}/{$date_folder}/";
- //1 .设置上传子目录(按日期分类)
- $expiration = date('Y-m-d\TH:i:s\Z', time() + 300); // 5分钟后过期
- $conditions = [
- ["content-length-range", 0, 1048576000], // 限制文件大小,这里设置为最大1GB
- ["starts-with", '$key', $dir] // 限制文件上传的目录前缀
- ];
- $policy = json_encode(['expiration' => $expiration, 'conditions' => $conditions]);
- $base64Policy = base64_encode($policy);
- // 2. 计算签名
- $signature = base64_encode(hash_hmac('sha1', $base64Policy, $accessKeySecret, true));
- // 3. 返回给前端的数据
- $response = [
- 'accessKeyId' => $accessKeyId,
- 'accessKeySecret'=>$accessKeySecret,
- 'bucket' => $bucket,
- 'region' => $region,
- 'dir' => $dir,
- 'policy' => $base64Policy,
- 'signature' => $signature,
- 'host' => $host,
- 'expire' => time() + 3600,
- 'oss_url'=>$this->oss_basic_url,
- 'show_url'=>$this->show_basie_url
- ];
- return [
- 'code'=>1,
- 'msg'=>'success',
- 'data'=>$response
- ];
- }
- }
|