Model_aliyunossnew.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Model_aliyunossnew extends Lin_Model
  3. {
  4. function __construct(){
  5. parent::__construct();
  6. }
  7. private $oss_basic_url = "https://lyerp-product-source.oss-cn-beijing.aliyuncs.com";
  8. private $show_basie_url ="https://lyerposs.wepolicy.cn";
  9. private function get_oss_config(){
  10. $query1 = $this->db->get_where('setting',array('skey' => 'oss_aliyun_id'));
  11. $oss_aliyun_id_info = $query1->row_array();
  12. $query2 = $this->db->get_where('setting',array('skey' => 'oss_aliyun_key'));
  13. $oss_aliyun_key_info = $query2->row_array();
  14. $oss_aliyun_id_info = $this->setting->find('skey = "oss_aliyun_id"');
  15. $oss_aliyun_key_info = $this->setting->find('skey = "oss_aliyun_key"');
  16. $accessKeyId = $oss_aliyun_id_info['svalue'];
  17. $accessKeySecret = $oss_aliyun_key_info['svalue'];
  18. $bucket = "lyerp-product-source";
  19. $endpoint = "oss-cn-beijing.aliyuncs.com";
  20. $region = "oss-cn-beijing";
  21. $host = $bucket.".".$endpoint;
  22. return [
  23. 'accessKeyId' => $accessKeyId,
  24. 'accessKeySecret' => $accessKeySecret,
  25. 'bucket' => $bucket,
  26. 'endpoint' => $endpoint,
  27. 'region' => $region,
  28. 'host' => $host,
  29. ];
  30. }
  31. public function getOssSign(){
  32. $conf = $this->get_oss_config();
  33. $accessKeyId = $conf['accessKeyId'];
  34. $accessKeySecret = $conf['accessKeySecret'];
  35. $region = $conf['region'];
  36. $bucket = $conf['bucket'];
  37. $endpoint = $conf['endpoint'];
  38. $host = $conf['host'];
  39. // 1. 获取OSS的签名信息
  40. $date_folder = date('Ymd');
  41. $dir = "uploads/{$date_folder}/";
  42. //1 .设置上传子目录(按日期分类)
  43. $expiration = date('Y-m-d\TH:i:s\Z', time() + 300); // 5分钟后过期
  44. $conditions = [
  45. ["content-length-range", 0, 1048576000], // 限制文件大小,这里设置为最大1GB
  46. ["starts-with", '$key', $dir] // 限制文件上传的目录前缀
  47. ];
  48. $policy = json_encode(['expiration' => $expiration, 'conditions' => $conditions]);
  49. $base64Policy = base64_encode($policy);
  50. // 2. 计算签名
  51. $signature = base64_encode(hash_hmac('sha1', $base64Policy, $accessKeySecret, true));
  52. // 3. 返回给前端的数据
  53. $response = [
  54. 'accessKeyId' => $accessKeyId,
  55. 'accessKeySecret'=>$accessKeySecret,
  56. 'bucket' => $bucket,
  57. 'region' => $region,
  58. 'dir' => $dir,
  59. 'policy' => $base64Policy,
  60. 'signature' => $signature,
  61. 'host' => $host,
  62. 'expire' => time() + 3600,
  63. 'oss_url'=>$this->oss_basic_url,
  64. 'show_url'=>$this->show_basie_url
  65. ];
  66. return [
  67. 'code'=>1,
  68. 'msg'=>'success',
  69. 'data'=>$response
  70. ];
  71. }
  72. //对于存到阿里云的oss 进行类别区分
  73. public function getOssSignType($type){
  74. if(empty($type)){
  75. return [
  76. "code"=>-1,
  77. "msg"=>"存储类别不对",
  78. "data"=>[]
  79. ];
  80. }
  81. $conf = $this->get_oss_config();
  82. $accessKeyId = $conf['accessKeyId'];
  83. $accessKeySecret = $conf['accessKeySecret'];
  84. $region = $conf['region'];
  85. $bucket = $conf['bucket'];
  86. $endpoint = $conf['endpoint'];
  87. $host = $conf['host'];
  88. // 1. 获取OSS的签名信息
  89. $date_folder = date('Ymd');
  90. $dir = "{$type}/{$date_folder}/";
  91. //1 .设置上传子目录(按日期分类)
  92. $expiration = date('Y-m-d\TH:i:s\Z', time() + 300); // 5分钟后过期
  93. $conditions = [
  94. ["content-length-range", 0, 1048576000], // 限制文件大小,这里设置为最大1GB
  95. ["starts-with", '$key', $dir] // 限制文件上传的目录前缀
  96. ];
  97. $policy = json_encode(['expiration' => $expiration, 'conditions' => $conditions]);
  98. $base64Policy = base64_encode($policy);
  99. // 2. 计算签名
  100. $signature = base64_encode(hash_hmac('sha1', $base64Policy, $accessKeySecret, true));
  101. // 3. 返回给前端的数据
  102. $response = [
  103. 'accessKeyId' => $accessKeyId,
  104. 'accessKeySecret'=>$accessKeySecret,
  105. 'bucket' => $bucket,
  106. 'region' => $region,
  107. 'dir' => $dir,
  108. 'policy' => $base64Policy,
  109. 'signature' => $signature,
  110. 'host' => $host,
  111. 'expire' => time() + 3600,
  112. 'oss_url'=>$this->oss_basic_url,
  113. 'show_url'=>$this->show_basie_url
  114. ];
  115. return [
  116. 'code'=>1,
  117. 'msg'=>'success',
  118. 'data'=>$response
  119. ];
  120. }
  121. }