阿里云sts token生成


获取配置

 

通过上传流程拿到 rolearn , rolesessionname 

 

然后在账户accessKey管理中 拿到 access_key_id,access_key_secret

 

 

 

 

拿到配置 在项目中过引入阿里云oss sdk包

composer require aliyuncs/oss-sdk-php

 

实例:

namespace common\service\storage\aliyun;

use AlibabaCloud\Client\AlibabaCloud;

class StsService
{
    protected $accessKeyId;
    protected $accessKeySecret;
    protected $roleArn;
    protected $roleSessionName;

    /**
     * @return mixed
     */
    public function getAccessKeyId()
    {
        return $this->accessKeyId;
    }

    /**
     * @param  mixed  $accessKeyId
     */
    public function setAccessKeyId($accessKeyId): void
    {
        $this->accessKeyId = $accessKeyId;
    }

    /**
     * @return mixed
     */
    public function getAccessKeySecret()
    {
        return $this->accessKeySecret;
    }

    /**
     * @param  mixed  $accessKeySecret
     */
    public function setAccessKeySecret($accessKeySecret): void
    {
        $this->accessKeySecret = $accessKeySecret;
    }

    /**
     * @return mixed
     */
    public function getRoleArn()
    {
        return $this->roleArn;
    }

    /**
     * @param  mixed  $roleArn
     */
    public function setRoleArn($roleArn): void
    {
        $this->roleArn = $roleArn;
    }

    /**
     * @return mixed
     */
    public function getRoleSessionName()
    {
        return $this->roleSessionName;
    }

    /**
     * @param  mixed  $roleSessionName
     */
    public function setRoleSessionName($roleSessionName): void
    {
        $this->roleSessionName = $roleSessionName;
    }


    public function getToken()
    {
        AlibabaCloud::accessKeyClient($this->getAccessKeyId(), $this->getAccessKeySecret())
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        $result = AlibabaCloud::rpc()
            ->product('Sts')
             ->scheme('https') // https | http
            ->version('2015-04-01')
            ->action('AssumeRole')
            ->method('POST')
            ->host('sts.aliyuncs.com')
            ->options([
                'query' => [
                    'RegionId' => "cn-hangzhou",
                    'RoleArn' => $this->getRoleArn(),
                    'RoleSessionName' => $this->getRoleSessionName(),
                ],
            ])
            ->request();

        return $result->toArray();
    }
}

 

返回结果示例

{
        "RequestId": "DF87C054-3C8C-5991-8736-C6A0A3B987341",
        "AssumedRoleUser": {
            "Arn": "acs:ram::162140990471573371:role/aliyunosstokengeneratorrole/external-username",
            "AssumedRoleId": "38840772692700903494:external-username"
        },
        "Credentials": {
            "SecurityToken": "CAISiwJ1q6Ft5B2yfSjIr5fYOMv11OxLgKCmQUL3klQ9SdZni6jCsDz2IHBEeHFvBuoYv/4wnGhZ7vcelrh+W4NIX0rNaY5t9ZlN9wqkbtJJfBBAVORW5qe+EE2/VjTZvqaLEcibIfrZfvCyESOm8gZ43br9cxi7QlWhKufnoJV7b9MRLGLaBHg8c7UwHAZ5r9IAPnb8LOukNgWQ4lDdF011oAFx+wgdgOadupTCsEOC1AyrkrNK/tmueceeApMybMslYbCcx/drc6fN6ilU5iVR+b1+5K4+omeX4onCXAMPsk/Za7KFqowzNnxwYq5krBqhDt+Pgkv51vOPekYntwgpKJ/tSVynP9SdXYz15gIkagAGGZPk4lswkKAyYJea0CFCnYlo4U44l5B/JQq44Z3P+qn5VwdxpBMoHd/ih13x8m/8g8vef1/7rbMvOK6iZDngYK5Xt7iESoJzAwtZENfhreGK6vuI1HEJ7W84b9wNJmOHZR4SltcbpFq6fNxJfgPR8f41vNdQ3PUNGSujpj0EtiQA==",
            "AccessKeyId": "STS.NTmsqA93j7bMCdFvTh3EYKdmiR",
            "AccessKeySecret": "6GuvTWhfHDK1S5k8mdWJe3rR3X8t4vMWbX6wgQgwUwDfE",
            "Expiration": "2022-12-15T02:46:29Z"
        }
    }

 

发布时间 : 2023-03-01,阅读量:1380
本文链接:https://upwqy.com/details/14.html
PHP 获取访问来源 thinkphp-queue 学习笔记