PHP code example of wlfpanda1012 / easy-sts

1. Go to this page and download the library: Download wlfpanda1012/easy-sts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wlfpanda1012 / easy-sts example snippets




declare(strict_types=1);

use Wlfpanda1012\EasySts\Adapter\AliyunStsAdapterFactory;
use Wlfpanda1012\EasySts\Adapter\CtyunStsAdapterFactory;
use Wlfpanda1012\EasySts\Adapter\LocalStsAdapterFactory;

use function Hyperf\Support\env;

return [
    // 默认配置
    // 预留了非公网环境的STS使用可能性(未来项目需求,暂未开发)
    // 配置 default 修改为 aliyun 或者 ctyun
    // 配置好对应的环境变量即可
    'default' => 'local',
    'adapter' => [
        'local' => [
            'driver' => LocalStsAdapterFactory::class,
            'accessId' => 'username',
            'accessSecret' => 'password',
        ],
        'aliyun' => [
            'driver' => AliyunStsAdapterFactory::class,
            'accessId' => env('ALIYUN_STS_ACCESS_ID'),
            'accessSecret' => env('ALIYUN_STS_ACCESS_SECRET'),
            'roleArn' => env('ALIYUN_STS_ROLE_ARN', 'acs:ram::xxxxxxxxxxx:role/xxx'),
            'roleSessionName' => env('ALIYUN_STS_ROLE_SESSION_NAME', 'xxxxxxxxxxx'),
            'endpoint' => env('ALIYUN_STS_ENDPOINT', 'sts.cn-hangzhou.aliyuncs.com'),
            'durationSeconds' => env('ALIYUN_STS_DURATION_SECONDS', 3600),
            'externalId' => env('ALIYUN_STS_EXTERNAL_ID', 'external_id_test'),
        ],
        'ctyun' => [
            'driver' => CtyunStsAdapterFactory::class,
            'accessId' => env('CTYUN_STS_ACCESS_ID'),
            'accessSecret' => env('CTYUN_STS_ACCESS_SECRET'),
            'endpoint' => env('CTYUN_STS_ENDPOINT', 'oos-cn-iam.ctyunapi.cn'),
            'durationSeconds' => env('CTYUN_STS_DURATION_SECONDS', 3600),
        ],
    ],
];

use Wlfpanda1012\CommonSts\Sts;

$sts = new Sts();
$policy = [];
$sts->getToken()
$token = $sts->getToken($policy);

use Wlfpanda1012\EasySts\StsFactory;

$sts = (new (StsFactory::class))->get('ctyun');
$policy = [];
$token = $sts->getToken($policy);

use Wlfpanda1012\EasySts\StsFactory;


$sts = new Sts();
$policy = $sts->storagePolicy(OSSEffect::ALLOW->value, [OSSAction::ALL_GET->value], $urls, $config);
$token = $sts->getToken($policy);
shell
php bin/hyperf.php vendor:publish wlfpanda1012/easy-sts