写了那么多,看了好多天,实在看不懂到底在讲些什么,根本就没讲到重点,demo代码也写得太烂:
关于RAM的STS,代码不断变化,版本号同样是2015-04-01的调用代码,结果aliyun-java-sdk-sts-2.1.0和aliyun-java-sdk-sts-2.1.6的代码根本就没有任何联系,下载下来的PDF说明也是一样。
以下是aliyun_java_sdk_sts_20150825的demo代码,一点说明都没有,这就是demo?使劲地去翻看你们的文档,一直就找不到roleSessionName到底是什么鬼,可能实在是在下太过蠢笨了吧,这么高深的代码一点都看不懂。另外,一篇求助文章,发表了好几天了,结果回复一直为0(除了我自己)!收费倒是收的很勤,看来大概是店大要欺客了吧~
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
public class StsServiceSample {
public static final String REGION_CN_HANGZHOU = "cn-hangzhou";
public static final String STS_API_VERSION = "2015-04-01";
public static final String STS_VERSION = "1";
static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,
String roleArn, String roleSessionName, String policy, ProtocolType protocolType) throws ClientException {
try {
IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setVersion(STS_API_VERSION);
request.setMethod(MethodType.POST);
request.setProtocol(protocolType);
request.setRoleArn(roleArn);
request.setRoleSessionName(roleSessionName);
request.setPolicy(policy);
final AssumeRoleResponse response = client.getAcsResponse(request);
return response;
} catch (ClientException e) {
throw e;
}
}
public static void main(String[] args) {
String accessKeyId = "o************F";
String accessKeySecret = "y*******************U";
String roleArn = "acs:ram::145883****900618:role/ossadminrole";
String roleSessionName = "alice";
String policy = "{\n" +
" \"Version\": \"1\", \n" +
" \"Statement\": [\n" +
" {\n" +
" \"Action\": [\n" +
" \"oss:GetBucket\", \n" +
" \"oss:GetObject\" \n" +
" ], \n" +
" \"Resource\": [\n" +
" \"acs:oss:*:177530****529849:mybucket\", \n" +
" \"acs:oss:*:177530****529849:mybucket/*\" \n" +
" ], \n" +
" \"Effect\": \"Allow\"\n" +
" }\n" +
" ]\n" +
"}";
ProtocolType protocolType = ProtocolType.HTTPS;
try {
final AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret,
roleArn, roleSessionName, policy, protocolType);
System.out.println("Expiration: " + response.getCredentials().getExpiration());
System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
} catch (ClientException e) {
System.out.println("Failed to get a federation token.");
System.out.println("Error code: " + e.getErrCode());
System.out.println("Error message: " + e.getErrMsg());
}
}
}
-------------------------
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。