84 lines
3.0 KiB
PHP
84 lines
3.0 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 [];
|
||
}
|
||
|
||
public function signatureUpload($file)
|
||
{
|
||
$config = config('filesystems.disks.aliyun');
|
||
$host = (data_get($config, 'use_ssl') ? 'https://' : 'http://') . data_get($config, 'bucket') . '.' . data_get($config, 'endpoint');
|
||
$data = [
|
||
"accessid" => $this->accessId,
|
||
"host" => $host,
|
||
"policy" => "eyJleHBpcmF0aW9uIjoiMjAxNS0xMS0wNVQyMDoyMzoyM1oiLCJjxb25kaXRpb25zIjpbWyJjcb250ZW50LWxlbmd0aC1yYW5nZSIsMCwxMDQ4NTc2MDAwXSxbInN0YXJ0cy13aXRoIiwiJGtleSIsInVzZXItZGlyXC8i****",
|
||
"signature" => "VsxOcOudx******z93CLaXPz+4s=",
|
||
"expire" => 1446727949,
|
||
"dir" => $file,
|
||
];
|
||
}
|
||
}
|