123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace app\api\controller;
- use AlibabaCloud\SDK\Sts\V20150401\Sts;
- use \Exception;
- use AlibabaCloud\Tea\Exception\TeaError;
- use AlibabaCloud\Tea\Utils\Utils;
- use Darabonba\OpenApi\Models\Config;
- use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleRequest;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- use AlibabaCloud\SDK\Vod\V20170321\Models\GetVideoPlayAuthRequest;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- require_once '../vendor/aliyunmail/aliyun-php-sdk-core/Config.php'; // 假定您的源码文件和aliyun-php-sdk处于同一目录
- $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
- if (file_exists($path)) require_once $path;
- date_default_timezone_set('PRC');
- /**
- * @title OssManage
- * @controller OssManage
- * @group base
- */
- class OssManage extends Base
- {
- protected $access_key = 'LTAI5tGJE1WU5ytYK2zBxnDx';// key
- protected $access_secret = 'uNyByodVajsN1lrMSCxpbrJF46Pwx3';//secret
- protected $role_arn= 'acs:ram::1122764885952286:role/ramoss';//role_arn
- protected $role_session_name= 'RamOss';//role_session_name
- /**
- * 使用AK&SK初始化账号Client
- * @param string $accessKeyId
- * @param string $accessKeySecret
- * @return Sts Client
- */
- public static function createClient($accessKeyId, $accessKeySecret){
- $config = new Config([
- // 您的 AccessKey ID
- "accessKeyId" => $accessKeyId,
- // 您的 AccessKey Secret
- "accessKeySecret" => $accessKeySecret
- ]);
- // 访问的域名
- $config->endpoint = "sts.cn-hangzhou.aliyuncs.com";
- return new Sts($config);
- }
- /**
- * @title 获取STS临时身份凭证
- * @desc 获取STS临时身份凭证
- * @author qc
- * @url /api/Oss_manage/getOssCredentials
- * @method GET
- * @tag 视频播放凭证
- * @header name:Authorization require:1 desc:Token
- * @return name:RequestId type:string default:-- desc:RequestId
- * @return name:Arn type:string default:-- desc:RoleArn(Arn)
- * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId
- * @return name:SecurityToken type:string default:-- desc:SecurityToken
- * @return name:AccessKeyId type:string default:-- desc:AccessKeyId
- * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret
- * @return name:Expiration type:string default:-- desc:Expiration
- */
- public function getOssCredentials()
- {
- $client = self::createClient($this->access_key,$this->access_secret);
- $assumeRoleRequest = new AssumeRoleRequest([
- 'roleArn' => $this->role_arn,
- 'roleSessionName' =>$this->role_session_name,
- ]);
- $runtime = new RuntimeOptions([]);
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->assumeRoleWithOptions($assumeRoleRequest, $runtime);
- $json_res = json_encode($res,true);
- $res_arr = json_decode($json_res,true);
- }
- catch (Exception $error) {
- if (!($error instanceof TeaError)) {
- $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
- }
- $this->error($error->message);
- }
- $this->success('ok',$res_arr['body'] );
- }
- /**
- * @title 获取STS临时身份凭证
- * @desc 获取STS临时身份凭证
- * @author qc
- * @url /api/Oss_manage/getOssCredential2
- * @method GET
- * @tag 视频播放凭证
- * @header name:Authorization require:1 desc:Token
- * @return name:RequestId type:string default:-- desc:RequestId
- * @return name:Arn type:string default:-- desc:RoleArn(Arn)
- * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId
- * @return name:SecurityToken type:string default:-- desc:SecurityToken
- * @return name:AccessKeyId type:string default:-- desc:AccessKeyId
- * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret
- * @return name:Expiration type:string default:-- desc:Expiration
- */
- public function getOssCredential2(){
- //构建一个阿里云客户端,用于发起请求。
- //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。
- AlibabaCloud::accessKeyClient($this->access_key, $this->access_secret)->regionId('cn-hangzhou')->asDefaultClient();
- //设置参数,发起请求。
- try {
- $result = AlibabaCloud::rpc()
- ->product('Sts')
- ->scheme('https')
- ->version('2015-04-01')
- ->action('AssumeRole')
- ->method('POST')
- ->host('sts.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-hangzhou",
- 'RoleArn' => $this->role_arn,
- 'RoleSessionName' => $this->role_session_name
- ],
- ])
- ->request();
- $ret = $result->toArray();
- $this->success('请求成功',$ret);
- } catch (ClientException $e) {
- $this->error($e->getErrorMessage() . PHP_EOL);
- } catch (ServerException $e) {
- $this->error($e->getErrorMessage() . PHP_EOL);
- }
- }
- }
|