com.zopen.aliyun.vod.service.AliyunVodStsService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zopen-ato-starter Show documentation
Show all versions of zopen-ato-starter Show documentation
Alibaba Tencent And Others For Spring Boot.
package com.zopen.aliyun.vod.service;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.zopen.aliyun.exception.AliyunAssert;
import com.zopen.aliyun.exception.AliyunException;
import com.zopen.aliyun.vod.dto.AliyunVodStsResp;
import com.zopen.ato.properties.AliyunVodProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("atoAliyunVodStsService")
public class AliyunVodStsService {
private static final Logger logger = LoggerFactory.getLogger(AliyunVodStsService.class);
@Autowired
private AliyunVodProperties aliyunVodProperties;
/**
* 获取STS临时AK
*
* @param roleArn 角色ARN,在阿里云控制台获取
* @return com.zopen.aliyun.vod.dto.AliyunVodStsResp
*/
public AliyunVodStsResp assumeRole(String roleArn) {
AliyunAssert.notNullAndEmpty(roleArn, "roleArn 不能为空");
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setSysEndpoint("sts." + AliyunVodProperties.REGION_ID + ".aliyuncs.com");
request.setSysMethod(MethodType.POST);
request.setRoleArn(roleArn);
request.setRoleSessionName("my-session-name");
try {
logger.debug(">>获取STS信息请求开始");
IClientProfile profile = DefaultProfile.getProfile(AliyunVodProperties.REGION_ID, aliyunVodProperties.getAccessKeyId(), aliyunVodProperties.getAccessKeySecret());
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleResponse response = client.getAcsResponse(request);
AliyunVodStsResp aliyunVodStsResp = new AliyunVodStsResp(response);
logger.debug("<<获取STS信息成功");
return aliyunVodStsResp;
} catch (ClientException e) {
logger.error("<<获取STS信息失败:errorCode[{}] errorMessage[{}]", e.getErrCode(), e.getErrMsg());
throw new AliyunException("获取授权信息失败");
} catch (Exception e) {
logger.error("<<获取STS信息失败:" + e.getMessage(), e);
throw new AliyunException("获取授权信息失败");
}
}
}