library-picturebook-activity/oss-direct-upload-demo/backend/OssConfig.java

80 lines
1.9 KiB
Java
Raw Permalink Normal View History

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", ".svg",
".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;
}
}