Model_aliyunoss.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Model_aliyunoss extends Lin_Model
  3. {
  4. function __construct(){
  5. parent::__construct();
  6. $this->load->_model(" Model_setting","setting");
  7. }
  8. private $oss_basic_url = "https://lyerp-product-source.oss-cn-beijing.aliyuncs.com";
  9. private $show_basie_url ="http://lyerposs.wepolicy.cn";
  10. private function get_oss_config(){
  11. $oss_aliyun_id_info = $this->setting->find('skey = "oss_aliyun_id"');
  12. $oss_aliyun_key_info = $this->setting->find('skey = "oss_aliyun_key"');
  13. $accessKeyId = $oss_aliyun_id_info['svalue'];
  14. $accessKeySecret = $oss_aliyun_key_info['svalue'];
  15. $bucket = "lyerp-product-source";
  16. $endpoint = "oss-cn-beijing.aliyuncs.com";
  17. $region = "oss-cn-beijing";
  18. $host = $bucket.".".$endpoint;
  19. return [
  20. 'accessKeyId' => $accessKeyId,
  21. 'accessKeySecret' => $accessKeySecret,
  22. 'bucket' => $bucket,
  23. 'endpoint' => $endpoint,
  24. 'region' => $region,
  25. 'host' => $host,
  26. ];
  27. }
  28. public function getOssSign(){
  29. $conf = $this->get_oss_config();
  30. $accessKeyId = $conf['accessKeyId'];
  31. $accessKeySecret = $conf['accessKeySecret'];
  32. $region = $conf['region'];
  33. $bucket = $conf['bucket'];
  34. $endpoint = $conf['endpoint'];
  35. $host = $conf['host'];
  36. // 1. 获取OSS的签名信息
  37. $date_folder = date('Ymd');
  38. $dir = "uploads/{$date_folder}/";
  39. //1 .设置上传子目录(按日期分类)
  40. $expiration = date('Y-m-d\TH:i:s\Z', time() + 300); // 5分钟后过期
  41. $conditions = [
  42. ["content-length-range", 0, 1048576000], // 限制文件大小,这里设置为最大1GB
  43. ["starts-with", '$key', $dir] // 限制文件上传的目录前缀
  44. ];
  45. $policy = json_encode(['expiration' => $expiration, 'conditions' => $conditions]);
  46. $base64Policy = base64_encode($policy);
  47. // 2. 计算签名
  48. $signature = base64_encode(hash_hmac('sha1', $base64Policy, $accessKeySecret, true));
  49. // 3. 返回给前端的数据
  50. $response = [
  51. 'accessKeyId' => $accessKeyId,
  52. 'accessKeySecret'=>$accessKeySecret,
  53. 'bucket' => $bucket,
  54. 'region' => $region,
  55. 'dir' => $dir,
  56. 'policy' => $base64Policy,
  57. 'signature' => $signature,
  58. 'host' => $host,
  59. 'expire' => time() + 3600,
  60. 'oss_url'=>$this->oss_basic_url,
  61. 'show_url'=>$this->show_basie_url
  62. ];
  63. return [
  64. 'code'=>1,
  65. 'msg'=>'success',
  66. 'data'=>$response
  67. ];
  68. }
  69. }