Model_aliyunossnew.php 4.7 KB

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