jiajialu / aliyun-sdk-sts
aliyun-php-sdk-sts 封装,支持composer方式使用
3.0.0
2018-12-14 09:11 UTC
Requires
- php: >=5.4
- jiajialu/aliyun-sdk-core: ^1.0
Requires (Dev)
- phpunit/phpunit: >=4.8 < 6.0
This package is not auto-updated.
Last update: 2025-04-01 05:48:01 UTC
README
项目介绍
阿里云SDK临时密钥授权STS管理包重构,支持composer方式使用
使用说明
<?php
require "vendor/autoload.php";
use AliCloud\Core\Profile\DefaultProfile;
use AliCloud\Core\DefaultAcsClient;
use AliCloud\Core\Exception\ServerException;
use AliCloud\Core\Exception\ClientException;
use AliCloud\STS\AssumeRoleRequest;
define("REGION_ID", "cn-shanghai");
define("ENDPOINT", "sts.cn-shanghai.aliyuncs.com"); //根据实际情况更改配置
// 只允许子用户使用角色
DefaultProfile::addEndpoint(REGION_ID, REGION_ID, "Sts", ENDPOINT);
$profile = DefaultProfile::getProfile(REGION_ID, "<acccess-key-id>", "<access-key-secret>");
$client = new DefaultAcsClient($profile);
// 角色资源描述符,在RAM的控制台的资源详情页上可以获取
$roleArn = "<role-arn>";
// 在扮演角色(AssumeRole)时,可以附加一个授权策略,进一步限制角色的权限;
// 详情请参考《RAM使用指南》
// 此授权策略表示读取所有OSS的只读权限
$policy=<<<POLICY
{
"Statement": [
{
"Action": [
"oss:Get*",
"oss:List*"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "1"
}
POLICY;
$request = new AssumeRoleRequest();
// RoleSessionName即临时身份的会话名称,用于区分不同的临时身份
// 您可以使用您的客户的ID作为会话名称
$request->setRoleSessionName("client_name");
$request->setRoleArn($roleArn);
$request->setPolicy($policy);
$request->setDurationSeconds(3600);
try {
$response = $client->getAcsResponse($request);
print_r($response);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}
在您使用STS的SDK前,请仔细阅读RAM使用指南中的角色管理部分,并阅读STS的API文档
更多用法参考aliyun官方文档:https://help.aliyun.com/document_detail/28756.html
参与贡献
- Fork 本项目
- 新建 Feat_xxx 分支
- 提交代码
- 新建 Pull Request