123
This commit is contained in:
@@ -338,3 +338,14 @@ dromara:
|
|||||||
base-path: dk-plus/ # 基础路径
|
base-path: dk-plus/ # 基础路径
|
||||||
path-patterns: /dk-plus/** # 访问路径
|
path-patterns: /dk-plus/** # 访问路径
|
||||||
storage-path: D:/Temp/ # 存储路径
|
storage-path: D:/Temp/ # 存储路径
|
||||||
|
ali:
|
||||||
|
oss:
|
||||||
|
enable: false
|
||||||
|
access-key-id: key
|
||||||
|
access-key-secret: secret
|
||||||
|
endpoint: cn-hangzhou
|
||||||
|
bucket-name: nono
|
||||||
|
duration-seconds: 3600
|
||||||
|
role-arn: acs:ram::12312321312:role/ramoss
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -47,5 +47,12 @@
|
|||||||
<groupId>org.freemarker</groupId>
|
<groupId>org.freemarker</groupId>
|
||||||
<artifactId>freemarker</artifactId>
|
<artifactId>freemarker</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.aliyun/sts20150401 -->
|
||||||
|
<!-- <dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>aliyun-java-sdk-sts</artifactId>
|
||||||
|
<version>3.1.2</version>
|
||||||
|
</dependency>-->
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,15 +1,25 @@
|
|||||||
package com.bashi.dk.controller.app;
|
package com.bashi.dk.controller.app;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
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.common.core.domain.AjaxResult;
|
||||||
import com.bashi.dk.manager.FileUploadManager;
|
import com.bashi.dk.manager.FileUploadManager;
|
||||||
import com.bashi.dk.manager.FileUploadRes;
|
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.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.x.file.storage.core.FileInfo;
|
import org.dromara.x.file.storage.core.FileInfo;
|
||||||
import org.dromara.x.file.storage.core.FileStorageService;
|
import org.dromara.x.file.storage.core.FileStorageService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@@ -17,6 +27,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
/**
|
/**
|
||||||
* <p>created on 2021/7/13</p>
|
* <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
|
* @author zhangliang
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@@ -26,6 +38,19 @@ public class V2CommonController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private FileStorageService fileStorageService;
|
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")
|
@PostMapping("/v2/common/upload")
|
||||||
@ApiOperation("文件上传")
|
@ApiOperation("文件上传")
|
||||||
public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class AliOssKit {
|
|||||||
|
|
||||||
public final static String OSS_KEY_COMMON = "dk/common/";
|
public final static String OSS_KEY_COMMON = "dk/common/";
|
||||||
|
|
||||||
@PostConstruct
|
// @PostConstruct
|
||||||
public void init(){
|
public void init(){
|
||||||
if(!ossConfig.isEnable()){
|
if(!ossConfig.isEnable()){
|
||||||
log.error("未开启阿里云OSS配置");
|
log.error("未开启阿里云OSS配置");
|
||||||
|
|||||||
@@ -21,4 +21,9 @@ public class OssConfig {
|
|||||||
private String accessKeyId; // 连接keyId
|
private String accessKeyId; // 连接keyId
|
||||||
private String accessKeySecret; // 连接秘钥
|
private String accessKeySecret; // 连接秘钥
|
||||||
private String bucketName; // 需要存储的bucketName
|
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