一 配置概述
移动端配置 快速搭建移动应用直传服务 (aliyun.com)https://help.aliyun.com/document_detail/31920.html 配置获取STS权限
使用STS临时访问凭证访问OSS (aliyun.com)https://help.aliyun.com/document_detail/100624.htm?spm=a2c4g.11186623.0.0.15541cd5FVNiAy#concept-xzh-nzk-2gb 前端的跨域配置
安装 (aliyun.com)https://help.aliyun.com/document_detail/64041.htm?spm=a2c4g.11186623.0.0.155440c2mZddA9#concept-64041-zh
二 依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sts</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.30</version>
</dependency>
三 关键信息与示例代码
public Map<String, String> stsOSS(long duration) {
//构建一个阿里云客户端,用于发起请求。
//构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
AssumeRoleRequest request = new AssumeRoleRequest();
request.setRoleArn(arn);
request.setRoleSessionName(roleSessionName);
request.setDurationSeconds(duration);
AssumeRoleResponse roleResponse = null;
try {
roleResponse = client.getAcsResponse(request);
} catch (ClientException e) {
e.printStackTrace();
}
Map<String, String> keyMap = new HashMap<>();
String accessKeyId = roleResponse.getCredentials().getAccessKeyId();
keyMap.put("accessKeyId", accessKeyId);
String accessKeySecret = roleResponse.getCredentials().getAccessKeySecret();
keyMap.put("accessKeySecret", accessKeySecret);
String securityToken = roleResponse.getCredentials().getSecurityToken();
keyMap.put("securityToken", securityToken);
return keyMap;
}
参考:
Java示例 (aliyun.com)https://help.aliyun.com/document_detail/28786.htm?spm=a2c4g.11186623.0.0.b8324f77RLwN1Q#concept-rxg-yxv-xdb四 返回结果
过期时间范围:The Min/Max value of DurationSeconds is 15min/1hr.
{
"data": {
"requestId": "87F9FF9B-4474-5367-A021-75F8CC1877A8",
"credentials": {
"securityToken": "CAIS8wF1q6Ft5B2yfSjIr5fsCPHc1bd5woaSaFTys1o/Scpi2obnrDz2IHFEdXNtCO0asfw2lGtV6foblqNpQppCXlfYYNBstknjS5EpJNivgde8yJBZor/HcDHhJnyW9cvWZPqDP7G5U/yxalfCuzZuyL/hD1uLVECkNpv74vwOLK5gPG+CYCFBGc1dKyZ7tcYeLgGxD/u2NQPwiWeiZygB+CgE0DshsvzunZ3EukSB0AKqktV4/dqhfsKWCOB3J4p6XtuP2+h7S7HMyiY46WIRpfcn1vEVpGuY5ojNWAQOv0ycUfDd99p0NxN0fbQqr3nRPQtIDJQagAGXaPltRLY90UzTc7ITbdu0DWgLWXZpRtbhTqfyKl64fIlHM8p1gG4WolqLQjTz6IBjWFCjihZQ029NWEXJ/EZp8U/HPBk6twmP8Krbila/itVyDO3ciqXiVki/2CDF7qRbTimK8uJ5nT+/ulktC38hmO4vFEQcvgMzuaEIGa08lQ==",
"accessKeySecret": "652RQY9PSroK***bbvyDdrHtnxX6DBtZFrsGQpwc8",
"accessKeyId": "STS.NTYCKh8hXu***CWZjEEN5CLN",
"expiration": "2021-12-31T07:11:54Z"
},
"assumedRoleUser": {
"arn": "acs:ram::1063*****780:role/ramosstest/ramosstest",
"assumedRoleId": "398925****01835741:ramosstest"
}
},
"code": "10000",
"msg": "操作成功!"
}
最后生成的arn = 系统配置的arn+系统设置的roleSessionName