| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Model_aliyunoss extends Lin_Model
- {
- function __construct(){
- parent::__construct();
- $this->load->_model(" Model_setting","setting");
- }
- private $oss_basic_url = "https://lyerp-product-source.oss-cn-beijing.aliyuncs.com";
- private $show_basie_url ="http://lyerposs.wepolicy.cn";
- private function get_oss_config(){
- $oss_aliyun_id_info = $this->setting->find('skey = "oss_aliyun_id"');
- $oss_aliyun_key_info = $this->setting->find('skey = "oss_aliyun_key"');
- $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
- ];
- }
- }
|