OssManage.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\SDK\Sts\V20150401\Sts;
  4. use \Exception;
  5. use AlibabaCloud\Tea\Exception\TeaError;
  6. use AlibabaCloud\Tea\Utils\Utils;
  7. use Darabonba\OpenApi\Models\Config;
  8. use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleRequest;
  9. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  10. use AlibabaCloud\SDK\Vod\V20170321\Models\GetVideoPlayAuthRequest;
  11. use AlibabaCloud\Client\AlibabaCloud;
  12. use AlibabaCloud\Client\Exception\ClientException;
  13. use AlibabaCloud\Client\Exception\ServerException;
  14. require_once '../vendor/aliyunmail/aliyun-php-sdk-core/Config.php'; // 假定您的源码文件和aliyun-php-sdk处于同一目录
  15. $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
  16. if (file_exists($path)) require_once $path;
  17. date_default_timezone_set('PRC');
  18. /**
  19. * @title OssManage
  20. * @controller OssManage
  21. * @group base
  22. */
  23. class OssManage extends Base
  24. {
  25. protected $access_key = 'LTAI5tGJE1WU5ytYK2zBxnDx';// key
  26. protected $access_secret = 'uNyByodVajsN1lrMSCxpbrJF46Pwx3';//secret
  27. protected $role_arn= 'acs:ram::1122764885952286:role/ramoss';//role_arn
  28. protected $role_session_name= 'RamOss';//role_session_name
  29. /**
  30. * 使用AK&SK初始化账号Client
  31. * @param string $accessKeyId
  32. * @param string $accessKeySecret
  33. * @return Sts Client
  34. */
  35. public static function createClient($accessKeyId, $accessKeySecret){
  36. $config = new Config([
  37. // 您的 AccessKey ID
  38. "accessKeyId" => $accessKeyId,
  39. // 您的 AccessKey Secret
  40. "accessKeySecret" => $accessKeySecret
  41. ]);
  42. // 访问的域名
  43. $config->endpoint = "sts.cn-hangzhou.aliyuncs.com";
  44. return new Sts($config);
  45. }
  46. /**
  47. * @title 获取STS临时身份凭证
  48. * @desc 获取STS临时身份凭证
  49. * @author qc
  50. * @url /api/Oss_manage/getOssCredentials
  51. * @method GET
  52. * @tag
  53. * @header name:Authorization require:1 desc:Token
  54. * @return name:RequestId type:string default:-- desc:RequestId
  55. * @return name:Arn type:string default:-- desc:RoleArn(Arn)
  56. * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId
  57. * @return name:SecurityToken type:string default:-- desc:SecurityToken
  58. * @return name:AccessKeyId type:string default:-- desc:AccessKeyId
  59. * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret
  60. * @return name:Expiration type:string default:-- desc:Expiration
  61. */
  62. public function getOssCredentials()
  63. {
  64. $client = self::createClient($this->access_key,$this->access_secret);
  65. $assumeRoleRequest = new AssumeRoleRequest([
  66. 'roleArn' => $this->role_arn,
  67. 'roleSessionName' =>$this->role_session_name,
  68. ]);
  69. $runtime = new RuntimeOptions([]);
  70. try {
  71. // 复制代码运行请自行打印 API 的返回值
  72. $res = $client->assumeRoleWithOptions($assumeRoleRequest, $runtime);
  73. $json_res = json_encode($res,true);
  74. $res_arr = json_decode($json_res,true);
  75. }
  76. catch (Exception $error) {
  77. if (!($error instanceof TeaError)) {
  78. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  79. }
  80. $this->error($error->message);
  81. }
  82. $this->success('ok',$res_arr['body'] );
  83. }
  84. /**
  85. * @title 临时身份凭证【rpc方式】
  86. * @desc 临时身份凭证
  87. * @author qc
  88. * @url /api/Oss_manage/getOssCredential2
  89. * @method GET
  90. * @tag
  91. * @header name:Authorization require:1 desc:Token
  92. * @return name:RequestId type:string default:-- desc:RequestId
  93. * @return name:Arn type:string default:-- desc:RoleArn(Arn)
  94. * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId
  95. * @return name:SecurityToken type:string default:-- desc:SecurityToken
  96. * @return name:AccessKeyId type:string default:-- desc:AccessKeyId
  97. * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret
  98. * @return name:Expiration type:string default:-- desc:Expiration
  99. */
  100. public function getOssCredential2(){
  101. //构建一个阿里云客户端,用于发起请求。
  102. //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。
  103. AlibabaCloud::accessKeyClient($this->access_key, $this->access_secret)->regionId('cn-hangzhou')->asDefaultClient();
  104. //设置参数,发起请求。
  105. try {
  106. $result = AlibabaCloud::rpc()
  107. ->product('Sts')
  108. ->scheme('https')
  109. ->version('2015-04-01')
  110. ->action('AssumeRole')
  111. ->method('POST')
  112. ->host('sts.aliyuncs.com')
  113. ->options([
  114. 'query' => [
  115. 'RegionId' => "cn-hangzhou",
  116. 'RoleArn' => $this->role_arn,
  117. 'RoleSessionName' => $this->role_session_name
  118. ],
  119. ])
  120. ->request();
  121. $ret = $result->toArray();
  122. $this->success('请求成功',$ret);
  123. } catch (ClientException $e) {
  124. $this->error($e->getErrorMessage() . PHP_EOL);
  125. } catch (ServerException $e) {
  126. $this->error($e->getErrorMessage() . PHP_EOL);
  127. }
  128. }
  129. }