66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\ServiceTrait\Common;
|
|
|
|
use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleRequest;
|
|
use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleResponse;
|
|
use AlibabaCloud\SDK\Sts\V20150401\Sts;
|
|
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
|
|
use App\Lib\Log;
|
|
use Darabonba\OpenApi\Models\Config;
|
|
use Exception;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use function Hyperf\Config\config;
|
|
|
|
trait AliStsTrait
|
|
{
|
|
/**
|
|
* @var Log
|
|
*/
|
|
#[Inject]
|
|
protected Log $log;
|
|
|
|
/**
|
|
* 创建客户端
|
|
* @return Sts
|
|
*/
|
|
protected function createClient(): Sts
|
|
{
|
|
$config = new Config([
|
|
"accessKeyId" => config('ali.access_key_id'),
|
|
"accessKeySecret" => config('ali.access_key_secret')
|
|
]);
|
|
$config->endpoint = config('ali.sts_endpoint');
|
|
|
|
return new Sts($config);
|
|
}
|
|
|
|
/**
|
|
* 获取 ali sts 控制器
|
|
* @param $payload
|
|
* @return AssumeRoleResponse
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function getAliStsControls($payload): AssumeRoleResponse
|
|
{
|
|
$client = $this->createClient();
|
|
$assumeRoleRequest = new AssumeRoleRequest($payload);
|
|
$runtime = new RuntimeOptions([]);
|
|
try {
|
|
return $client->assumeRoleWithOptions($assumeRoleRequest, $runtime);
|
|
} catch (Exception $error) {
|
|
$this->log->error(__CLASS__.'-'.__FUNCTION__.'-'.__LINE__.':'.$error->getMessage());
|
|
}
|
|
}
|
|
} |