2026-04-08 15:19:43 +08:00
|
|
|
|
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 Endpoint(如:oss-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",
|
2026-04-15 11:43:35 +08:00
|
|
|
|
".mp4", ".avi", ".mov", ".wmv", ".svg",
|
2026-04-08 15:19:43 +08:00
|
|
|
|
".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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|