新增COS配置
This commit is contained in:
parent
cd95151b58
commit
d9e10b14a9
@ -0,0 +1,36 @@
|
||||
package com.ruoyi.web.controller.cos;
|
||||
|
||||
import com.ruoyi.system.service.CosStsService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.security.PermitAll;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description: sts
|
||||
* @Author: 86187
|
||||
* @Date: 2025/03/20 16:34
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/cos")
|
||||
@RequiredArgsConstructor
|
||||
public class CosStsController {
|
||||
|
||||
private final CosStsService cosStsService;
|
||||
|
||||
/**
|
||||
* 获取cos临时密钥
|
||||
*
|
||||
* @return sts
|
||||
*/
|
||||
@GetMapping("/sts")
|
||||
@PermitAll
|
||||
public Map<String, Object> getCosSts() {
|
||||
return cosStsService.getTempKeys();
|
||||
}
|
||||
}
|
@ -173,3 +173,11 @@ dl-rights:
|
||||
# 每月发布通告额度
|
||||
addNotice: 3
|
||||
report: 5
|
||||
#################### 腾讯COS相关配置 ####################
|
||||
cos:
|
||||
baseUrl: notice-1348525010.cos.ap-beijing.myqcloud.com
|
||||
accessKey: AKIDDbyY3Wr9D4i9LK6f085pLfleJlz60hAP
|
||||
secretKey: 82kJfnu11ulW5TghV5TecVYP3TghXAZl
|
||||
regionName: ap-beijing
|
||||
bucketName: notice-1348525010
|
||||
folderPrefix: /files
|
||||
|
@ -70,13 +70,13 @@
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- JSON工具类 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 阿里JSON解析器 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
@ -165,7 +165,12 @@
|
||||
<artifactId>mybatis-plus</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>cos-sts_api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
@ -0,0 +1,82 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import com.tencent.cloud.CosStsClient;
|
||||
import com.tencent.cloud.Response;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* @Description: sts
|
||||
* @Author: 86187
|
||||
* @Date: 2025/03/20 16:18
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Service
|
||||
public class CosStsService {
|
||||
// 你的腾讯云账号的 SecretId 和 SecretKey
|
||||
@Value("${cos.accessKey}")
|
||||
private String SECRET_ID;
|
||||
@Value("${cos.secretKey}")
|
||||
private String SECRET_KEY;
|
||||
|
||||
// 存储桶所属的地域,例如 "ap-shanghai"
|
||||
@Value("${cos.regionName}")
|
||||
private String REGION;
|
||||
|
||||
// 你的存储桶名,例如 "example-1250000000"
|
||||
@Value("${cos.bucketName}")
|
||||
private String BUCKET;
|
||||
|
||||
// 临时密钥有效期(单位秒),最长不超过 2 小时(7200 秒)
|
||||
private static final int DURATION_SECONDS = 1800;
|
||||
|
||||
public Map<String, Object> getTempKeys() {
|
||||
TreeMap<String, Object> config = new TreeMap<String, Object>();
|
||||
|
||||
try {
|
||||
// 云 api 密钥 SecretId
|
||||
config.put("secretId", SECRET_ID);
|
||||
// 云 api 密钥 SecretKey
|
||||
config.put("secretKey", SECRET_KEY);
|
||||
// 临时密钥有效时长,单位是秒
|
||||
config.put("durationSeconds", 1800);
|
||||
// 换成你的 bucket
|
||||
config.put("bucket", BUCKET);
|
||||
// 换成 bucket 所在地区
|
||||
config.put("region", REGION);
|
||||
|
||||
// 可以通过 allowPrefixes 指定前缀数组, 例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
|
||||
config.put("allowPrefixes", new String[] {
|
||||
"*"
|
||||
});
|
||||
|
||||
// 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
|
||||
String[] allowActions = new String[] {
|
||||
// 简单上传
|
||||
"name/cos:PutObject",
|
||||
"name/cos:PostObject",
|
||||
// 分片上传
|
||||
"name/cos:InitiateMultipartUpload",
|
||||
"name/cos:ListMultipartUploads",
|
||||
"name/cos:ListParts",
|
||||
"name/cos:UploadPart",
|
||||
"name/cos:CompleteMultipartUpload"
|
||||
};
|
||||
config.put("allowActions", allowActions);
|
||||
|
||||
Response response = CosStsClient.getCredential(config);
|
||||
Map<String, Object> result= new HashMap<>();
|
||||
result.put("credentials", response.credentials);
|
||||
result.put("startTime", response.startTime);
|
||||
result.put("expiredTime", response.expiredTime);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalArgumentException("no valid secret !");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user