123
This commit is contained in:
@@ -32,7 +32,7 @@ public class AliOssKit {
|
||||
|
||||
public final static String OSS_KEY_COMMON = "dk/common/";
|
||||
|
||||
@PostConstruct
|
||||
// @PostConstruct
|
||||
public void init(){
|
||||
if(!ossConfig.isEnable()){
|
||||
log.error("未开启阿里云OSS配置");
|
||||
|
||||
@@ -21,4 +21,9 @@ public class OssConfig {
|
||||
private String accessKeyId; // 连接keyId
|
||||
private String accessKeySecret; // 连接秘钥
|
||||
private String bucketName; // 需要存储的bucketName
|
||||
private Long durationSeconds = 3600L;
|
||||
private String roleArn;
|
||||
private String roleSessionName = "sts-session";
|
||||
|
||||
private String key = "upload/";
|
||||
}
|
||||
|
||||
76
bashi-dk/src/main/java/com/bashi/dk/oss/ali/StsOssKit.java
Normal file
76
bashi-dk/src/main/java/com/bashi/dk/oss/ali/StsOssKit.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package com.bashi.dk.oss.ali;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.auth.sts.AssumeRoleRequest;
|
||||
import com.aliyuncs.auth.sts.AssumeRoleResponse;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.aliyuncs.exceptions.ServerException;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class StsOssKit {
|
||||
|
||||
@Autowired
|
||||
@Setter
|
||||
@Getter
|
||||
private OssConfig ossConfig;
|
||||
|
||||
@Getter
|
||||
public IAcsClient acsClient = null;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
if(!ossConfig.isEnable()){
|
||||
log.error("未开启阿里云OSS配置");
|
||||
return;
|
||||
}
|
||||
DefaultProfile profile = DefaultProfile.getProfile(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
||||
acsClient = new DefaultAcsClient(profile);
|
||||
}
|
||||
|
||||
|
||||
public StsResult getStsToken(){
|
||||
// 创建API请求并设置参数
|
||||
AssumeRoleRequest request = new AssumeRoleRequest();
|
||||
request.setDurationSeconds(ossConfig.getDurationSeconds());
|
||||
request.setRoleArn(ossConfig.getRoleArn());
|
||||
request.setRoleSessionName(ossConfig.getRoleSessionName());
|
||||
try {
|
||||
AssumeRoleResponse response = acsClient.getAcsResponse(request);
|
||||
log.info(JSON.toJSONString(response));
|
||||
// 打印您需要的返回值,此处打印的是此次请求的 RequestId
|
||||
log.info(response.getRequestId());
|
||||
StsResult stsResult = new StsResult();
|
||||
stsResult.setToken(response.getCredentials().getSecurityToken());
|
||||
stsResult.setKeyId(response.getCredentials().getAccessKeyId());
|
||||
stsResult.setKeySecret(response.getCredentials().getAccessKeySecret());
|
||||
stsResult.setRegion("oss-"+ossConfig.getEndpoint());
|
||||
stsResult.setBucket(ossConfig.getBucketName());
|
||||
String nowStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
stsResult.setKey(ossConfig.getKey()+nowStr+"/");
|
||||
return stsResult;
|
||||
} catch (ServerException e) {
|
||||
log.error("上传文件系统异常",e);
|
||||
} catch (ClientException e) {
|
||||
// 打印错误码
|
||||
log.error("ErrCode:" + e.getErrCode());
|
||||
log.error("ErrMsg:" + e.getErrMsg());
|
||||
log.error("RequestId:" + e.getRequestId());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
13
bashi-dk/src/main/java/com/bashi/dk/oss/ali/StsResult.java
Normal file
13
bashi-dk/src/main/java/com/bashi/dk/oss/ali/StsResult.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.bashi.dk.oss.ali;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class StsResult {
|
||||
private String token;
|
||||
private String keyId;
|
||||
private String keySecret;
|
||||
private String region;
|
||||
private String bucket;
|
||||
private String key;
|
||||
}
|
||||
Reference in New Issue
Block a user