70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use AlibabaCloud\Client\AlibabaCloud;
|
||
use AlibabaCloud\Client\Exception\ClientException;
|
||
use AlibabaCloud\Client\Exception\ServerException;
|
||
use Illuminate\Support\Arr;
|
||
|
||
class AliStsService
|
||
{
|
||
protected $accessId;
|
||
|
||
protected $accessKey;
|
||
|
||
protected $regionId;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->accessId = config('filesystems.disks.aliyun.access_id', '');
|
||
$this->accessKey = config('filesystems.disks.aliyun.access_key', '');
|
||
$this->regionId = config('filesystems.disks.aliyun.region', '');
|
||
|
||
//构建一个阿里云客户端,用于发起请求。
|
||
//构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
|
||
AlibabaCloud::accessKeyClient($this->accessId, $this->accessKey)
|
||
->regionId($this->regionId)
|
||
->asDefaultClient();
|
||
}
|
||
|
||
/**
|
||
* 创建临时凭证
|
||
*
|
||
* @param string $sessionName
|
||
* @param integer $expireSeconds
|
||
* @return array
|
||
*/
|
||
public function createSts(string $sessionName, int $expireSeconds = 3600)
|
||
{
|
||
//设置参数,发起请求。关于参数含义和设置方法,请参见《API参考》。
|
||
try {
|
||
$result = AlibabaCloud::rpc()
|
||
->product('Sts')
|
||
->scheme('https') // https | http
|
||
->version('2015-04-01')
|
||
->action('AssumeRole')
|
||
->method('POST')
|
||
->host(config('filesystems.disks.aliyun.sts_host'))
|
||
->options([
|
||
'query' => [
|
||
'RegionId' => $this->regionId,
|
||
'RoleArn' => config('filesystems.disks.aliyun.sts_arn'),
|
||
'RoleSessionName' => $sessionName,
|
||
'DurationSeconds' => $expireSeconds,
|
||
],
|
||
])
|
||
->request();
|
||
return Arr::get($result->toArray(), 'Credentials', []);
|
||
} catch (ClientException $e) {
|
||
report($e);
|
||
// dd($e->getErrorMessage());
|
||
// echo $e->getErrorMessage() . PHP_EOL;
|
||
} catch (ServerException $e) {
|
||
report($e);
|
||
// echo $e->getErrorMessage() . PHP_EOL;
|
||
}
|
||
return [];
|
||
}
|
||
}
|