123
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
package com.bashi.dk.controller.app;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
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 com.bashi.common.core.domain.AjaxResult;
|
||||
import com.bashi.dk.manager.FileUploadManager;
|
||||
import com.bashi.dk.manager.FileUploadRes;
|
||||
import com.bashi.dk.oss.ali.StsOssKit;
|
||||
import com.bashi.dk.oss.ali.StsResult;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.x.file.storage.core.FileInfo;
|
||||
import org.dromara.x.file.storage.core.FileStorageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -17,6 +27,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
/**
|
||||
* <p>created on 2021/7/13</p>
|
||||
*
|
||||
* https://help.aliyun.com/zh/oss/developer-reference/use-temporary-access-credentials-provided-by-sts-to-access-oss#p-osc-r0m-u63
|
||||
*
|
||||
* @author zhangliang
|
||||
*/
|
||||
@RestController
|
||||
@@ -26,6 +38,19 @@ public class V2CommonController {
|
||||
@Autowired
|
||||
private FileStorageService fileStorageService;
|
||||
|
||||
@Autowired
|
||||
private StsOssKit stsOssKit;
|
||||
|
||||
@GetMapping("/v2/common/sts")
|
||||
@ApiOperation("文件上传")
|
||||
public AjaxResult getSts(){
|
||||
StsResult stsToken = stsOssKit.getStsToken();
|
||||
if(stsToken == null){
|
||||
return AjaxResult.error(400,"文件上传初始化失败");
|
||||
}
|
||||
return AjaxResult.success(stsToken);
|
||||
}
|
||||
|
||||
@PostMapping("/v2/common/upload")
|
||||
@ApiOperation("文件上传")
|
||||
public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
||||
|
||||
@@ -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