aliyun_sts.auto_cache.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * 将图片直接上传到OSS上,不中转;
  4. * 参考网址:https://help.aliyun.com/document_detail/31920.html?spm=5176.doc31931.6.206.OEtePt
  5. * status: 1,
  6. //上传文件时,必要的3个参数
  7. AccessKeyId: "",
  8. AccessKeySecret: "",
  9. SecurityToken: "",
  10. //过期时间,客户端不关心
  11. Expiration: "2016-09-28T10:30:02Z",
  12. //出错时,返回下面3个参数
  13. RequestId: "",
  14. Code: "",
  15. Message: "",
  16. //回调地址
  17. callbackUrl: "",
  18. callbackBody: "",
  19. //文件存放目录
  20. dir: ""
  21. */
  22. class aliyun_sts_auto_cache extends auto_cache{
  23. private $key = "aliyun:sts";
  24. public function load($param)
  25. {
  26. $rows = $GLOBALS['cache']->get($this->key);
  27. if($rows === false)
  28. {
  29. fanwe_require(APP_ROOT_PATH.'system/sts-server/aliyun-php-sdk-core/Config.php');
  30. fanwe_require(APP_ROOT_PATH.'system/sts-server/aliyun-php-sdk-sts/Sts/Request/V20150401/AssumeRoleRequest.php');
  31. $m_config = load_auto_cache("m_config");//初始化手机端配置
  32. $accessKeyID = $m_config['sts_access_key_id'];
  33. $accessKeySecret = $m_config['sts_access_key_secret'];;
  34. $roleArn = $m_config['sts_access_key_role_arn'];
  35. $tokenExpire = 3600;
  36. $policy = file_get_contents(APP_ROOT_PATH."system/sts-server/policy/bucket_upload_img_policy.txt");
  37. $OSS_BUCKET_NAME = $GLOBALS['distribution_cfg']['OSS_BUCKET_NAME'];
  38. $policy = str_replace('REPLACE_BUCKET_NAME', $OSS_BUCKET_NAME, $policy);
  39. //print_r($policy);
  40. $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $accessKeyID, $accessKeySecret);
  41. $client = new DefaultAcsClient($iClientProfile);
  42. $request = new AssumeRoleRequest();
  43. $request->setRoleSessionName("client_name");
  44. $request->setRoleArn($roleArn);
  45. $request->setPolicy($policy);
  46. $request->setDurationSeconds($tokenExpire);
  47. $response = $client->doAction($request);
  48. //print_r($response);
  49. $rows = array();
  50. $body = $response->getBody();
  51. $content = json_decode($body);
  52. $rows['status'] = $response->getStatus();
  53. $rows['sts_video_limit'] = intval($m_config['sts_video_limit']) ?: 60;
  54. if ($response->getStatus() == 200)
  55. {
  56. $rows['AccessKeyId'] = $content->Credentials->AccessKeyId;
  57. $rows['AccessKeySecret'] = $content->Credentials->AccessKeySecret;
  58. $rows['Expiration'] = $content->Credentials->Expiration;
  59. $rows['SecurityToken'] = $content->Credentials->SecurityToken;
  60. $rows['bucket'] = $OSS_BUCKET_NAME;
  61. $endpoint = $GLOBALS['distribution_cfg']['OSS_ENDPOINT'];
  62. if ($GLOBALS['distribution_cfg']['OSS_ENDPOINT_WITH_BUCKET_NAME']){
  63. $endpoint = str_replace($OSS_BUCKET_NAME.'.', '', $endpoint);
  64. }
  65. $endpoint = strtolower($endpoint);
  66. if (strpos($endpoint, 'http') === false){
  67. $endpoint = 'http://'.$endpoint;
  68. }
  69. $imgendpoint = $endpoint;
  70. $imgendpoint = str_replace('//oss-', '//img-', $imgendpoint);
  71. $rows['oss_domain'] = $GLOBALS['distribution_cfg']['OSS_DOMAIN'].'/';
  72. $rows['imgendpoint'] = $imgendpoint;
  73. $rows['endpoint'] = $endpoint;
  74. $rows['RequestId'] = '';
  75. $rows['Code'] = '';
  76. $rows['Message'] = '';
  77. $GLOBALS['cache']->set($this->key,$rows,$tokenExpire - 900);
  78. }
  79. else
  80. {
  81. $rows['AccessKeyId'] = "";
  82. $rows['AccessKeySecret'] = "";
  83. $rows['Expiration'] = "";
  84. $rows['SecurityToken'] = "";
  85. $rows['RequestId'] = $content->RequestId;
  86. $rows['Code'] = $content->Code;
  87. $rows['Message'] = $content->Message;
  88. //$rows['body'] = json_decode($body,1);
  89. }
  90. /*
  91. //回调说明https://help.aliyun.com/document_detail/31922.html?spm=5176.doc31921.6.208.xZgpik
  92. $rows['callbackUrl'] = '';
  93. $rows['callbackBody'] = '';
  94. //上传的目录是由服务端(即PHP)指定的,这样的好处就是安全。 这样就能控制每个客户端只能上传指定到指定的目录,做到安全隔离, 想要修改上传目录地址成abc/(必须以'/'结尾)
  95. $rows['dir'] = '';
  96. */
  97. }
  98. return $rows;
  99. }
  100. public function rm($param)
  101. {
  102. $GLOBALS['cache']->rm($this->key);
  103. }
  104. public function clear_all($param)
  105. {
  106. $GLOBALS['cache']->rm($this->key);
  107. }
  108. }
  109. ?>