diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/cos/CosStsController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/cos/CosStsController.java new file mode 100644 index 0000000..529f860 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/cos/CosStsController.java @@ -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 getCosSts() { + return cosStsService.getTempKeys(); + } +} diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index c2c809f..6d45895 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -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 diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index e985d7a..664dbd6 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -70,13 +70,13 @@ org.apache.commons commons-lang3 - + com.fasterxml.jackson.core jackson-databind - + com.alibaba.fastjson2 @@ -165,7 +165,12 @@ mybatis-plus 3.3.0 + + com.qcloud + cos-sts_api + 3.1.0 + - \ No newline at end of file + diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/CosStsService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/CosStsService.java new file mode 100644 index 0000000..7570b14 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/CosStsService.java @@ -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 getTempKeys() { + TreeMap config = new TreeMap(); + + 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 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 !"); + } + } +}