AliyunStsServices.php 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace common\services\mall\aliyunsts;
  3. use common\services\mall\aliyunsts\Sts\Sts\Request\V20150401\AssumeRoleRequest;
  4. use common\services\mall\aliyunsts\StsCode\DefaultAcsClient;
  5. use common\services\mall\aliyunsts\StsCode\Profile\DefaultProfile;
  6. use common\services\mall\aliyunsts\StsCode\Regions\Endpoint;
  7. use common\services\mall\aliyunsts\StsCode\Regions\EndpointProvider;
  8. use common\services\mall\aliyunsts\StsCode\Regions\ProductDomain;
  9. use Exception;
  10. class AliyunStsServices
  11. {
  12. public function getSts($mall_id, $params)
  13. {
  14. try {
  15. $accessKeyID = $params['storage_aliyun_accesskeyid'];
  16. $accessKeySecret = $params['storage_aliyun_accesskeysecret'];
  17. $roleArn = $params['arn'];
  18. $tokenExpire = 3600;
  19. $policy = '{
  20. "Statement": [
  21. {
  22. "Action": [
  23. "oss:*"
  24. ],
  25. "Effect": "Allow",
  26. "Resource": ["acs:oss:*"]
  27. }
  28. ],
  29. "Version": "1"
  30. }';
  31. $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $accessKeyID, $accessKeySecret);
  32. $this->setEndpoint("cn-hangzhou");
  33. // EndpointProvider::setEndpoints($params['storage_aliyun_endpoint']);
  34. $client = new DefaultAcsClient($iClientProfile);
  35. $request = new AssumeRoleRequest();
  36. $request->setRoleSessionName("bigVideo");
  37. $request->setRoleArn($roleArn);
  38. // $request->setPolicy($policy);
  39. $request->setDurationSeconds($tokenExpire);
  40. $response = $client->doAction($request);
  41. $rows = array();
  42. $body = $response->getBody();
  43. $content = json_decode($body, true);
  44. $rows['status'] = $response->getStatus();
  45. if ($rows['status'] != 200) throw new Exception($content['Message']);
  46. $rows['AccessKeyId'] = $content['Credentials']['AccessKeyId'];
  47. $rows['AccessKeySecret'] = $content['Credentials']['AccessKeySecret'];
  48. $rows['Expiration'] = $content['Credentials']['Expiration'];
  49. $rows['SecurityToken'] = $content['Credentials']['SecurityToken'];
  50. return $rows;
  51. } catch (\Exception $e) {
  52. throw new Exception($e->getMessage());
  53. }
  54. }
  55. public function setEndpoint($endpoint)
  56. {
  57. $regionIds = array("cn-hangzhou","cn-beijing","cn-qingdao","cn-hongkong","cn-shanghai","us-west-1","cn-shenzhen","ap-southeast-1");
  58. $productDomains =array(
  59. new ProductDomain("Ecs", "ecs.aliyuncs.com"),
  60. new ProductDomain("Rds", "rds.aliyuncs.com"),
  61. new ProductDomain("BatchCompute", "batchCompute.aliyuncs.com"),
  62. new ProductDomain("Bss", "bss.aliyuncs.com"),
  63. new ProductDomain("Oms", "oms.aliyuncs.com"),
  64. new ProductDomain("Slb", "slb.aliyuncs.com"),
  65. new ProductDomain("Oss", "oss-cn-hangzhou.aliyuncs.com"),
  66. new ProductDomain("OssAdmin", "oss-admin.aliyuncs.com"),
  67. new ProductDomain("Sts", "sts.aliyuncs.com"),
  68. new ProductDomain("Yundun", "yundun-cn-hangzhou.aliyuncs.com"),
  69. new ProductDomain("Risk", "risk-cn-hangzhou.aliyuncs.com"),
  70. new ProductDomain("Drds", "drds.aliyuncs.com"),
  71. new ProductDomain("M-kvstore", "m-kvstore.aliyuncs.com"),
  72. new ProductDomain("Ram", "ram.aliyuncs.com"),
  73. new ProductDomain("Cms", "metrics.aliyuncs.com"),
  74. new ProductDomain("Crm", "crm-cn-hangzhou.aliyuncs.com"),
  75. new ProductDomain("Ocs", "pop-ocs.aliyuncs.com"),
  76. new ProductDomain("Ots", "ots-pop.aliyuncs.com"),
  77. new ProductDomain("Dqs", "dqs.aliyuncs.com"),
  78. new ProductDomain("Location", "location.aliyuncs.com"),
  79. new ProductDomain("Ubsms", "ubsms.aliyuncs.com"),
  80. new ProductDomain("Ubsms-inner", "ubsms-inner.aliyuncs.com")
  81. );
  82. $endpoint = new Endpoint($endpoint, $regionIds, $productDomains);
  83. $endpoints = array($endpoint);
  84. EndpointProvider::setEndpoints($endpoints);
  85. }
  86. }