library-picturebook-activity/oss-direct-upload-demo/backend/OssConfig.java
En b9ed5e17c6 feat: OSS 客户端直传改造(STS Token 签发 + 前端直传 + CORS 自动配置)
后端新增 OssUtils/OssTokenVo/OssCorsInitRunner,通过 STS 临时凭证实现客户端直传 OSS;
前端 upload API 适配直传流程,赛事创建/作品提交/作业/富文本编辑器均已切换;
多环境(dev/test/prod) OSS 配置补全;新增 oss-direct-upload-demo 示例项目及 E2E 测试。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 15:19:43 +08:00

80 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.example.oss.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* 阿里云 OSS 配置类
* <p>
* 从 application.yml 中读取 aliyun.oss 前缀的配置项
* </p>
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssConfig {
/**
* OSS Endpointoss-cn-hangzhou.aliyuncs.com
*/
private String endpoint;
/**
* 访问密钥 ID
*/
private String accessKeyId;
/**
* 访问密钥秘密
*/
private String accessKeySecret;
/**
* Bucket 名称
*/
private String bucketName;
/**
* 文件最大大小(字节),默认 10MB
*/
private Long maxFileSize = 10 * 1024 * 1024L;
/**
* 是否在启动时自动配置 OSS Bucket CORS解决前端直传跨域
*/
private Boolean corsEnabled = false;
/**
* CORS 允许的来源逗号分隔http://localhost:5173,https://example.com
* 使用 * 表示允许所有来源
*/
private String corsAllowedOrigins = "http://localhost:5173,http://localhost:5174";
/**
* 允许的文件扩展名
*/
private String[] allowedExtensions = new String[]{
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp",
".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx",
".mp4", ".avi", ".mov", ".wmv",
".mp3", ".wav",
".txt"
};
/**
* 获取完整访问路径(带 Bucket
*
* @return 完整访问路径
*/
public String getFullEndpoint() {
if (endpoint == null) {
return null;
}
if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
return endpoint;
}
return "https://" + bucketName + "." + endpoint;
}
}