admin_config/
cos_config.rs

1//! 对象存储配置模块
2//!
3//! 提供多种云对象存储服务的配置,包括:
4//! - 腾讯云 COS
5//! - 阿里云 OSS
6//! - AWS S3
7//! - MinIO
8//! - 华为云 OBS
9//! - RustFS(本地文件系统)
10
11use serde::{Deserialize, Serialize};
12
13/// 对象存储配置
14///
15/// 支持多种云存储服务商和本地文件系统
16///
17/// # 字段说明
18///
19/// - `provider`: 存储提供商,可选值:tencent/aliyun/aws/minio/huawei/rustfs
20/// - `tencent`: 腾讯云 COS 配置
21/// - `aliyun`: 阿里云 OSS 配置
22/// - `aws`: AWS S3 配置
23/// - `minio`: MinIO 配置
24/// - `huawei`: 华为云 OBS 配置
25/// - `rustfs`: 本地文件系统配置
26///
27/// # 示例
28///
29/// ```rust
30/// use admin_config::CosConfig;
31///
32/// let config = CosConfig {
33///     provider: "tencent".to_string(),
34///     ..Default::default()
35/// };
36/// ```
37#[derive(Debug, Clone, Serialize, Deserialize, Default)]
38pub struct CosConfig {
39    /// 对象存储提供商 (tencent/aliyun/aws/minio/huawei/rustfs)
40    pub provider: String,
41    /// 腾讯云 COS 配置
42    #[serde(default)]
43    pub tencent: TencentCosConfig,
44    /// 阿里云 OSS 配置
45    #[serde(default)]
46    pub aliyun: AliyunOssConfig,
47    /// AWS S3 配置
48    #[serde(default)]
49    pub aws: AwsS3Config,
50    /// MinIO 配置
51    #[serde(default)]
52    pub minio: MinioConfig,
53    /// 华为云 OBS 配置
54    #[serde(default)]
55    pub huawei: HuaweiObsConfig,
56    /// RustFS 本地文件系统配置
57    #[serde(default)]
58    pub rustfs: RustFsConfig,
59}
60
61/// 腾讯云 COS 配置
62#[derive(Debug, Clone, Serialize, Deserialize, Default)]
63pub struct TencentCosConfig {
64    /// Secret ID
65    pub secret_id: String,
66    /// Secret Key
67    pub secret_key: String,
68    /// 存储桶名称
69    pub bucket: String,
70    /// 区域
71    pub region: String,
72    /// CDN 域名
73    pub cdn_domain: Option<String>,
74}
75
76/// 阿里云 OSS 配置
77#[derive(Debug, Clone, Serialize, Deserialize, Default)]
78pub struct AliyunOssConfig {
79    /// Access Key ID
80    pub access_key_id: String,
81    /// Access Key Secret
82    pub access_key_secret: String,
83    /// 存储桶名称
84    pub bucket: String,
85    /// 区域节点 (例如: oss-cn-hangzhou)
86    pub endpoint: String,
87    /// CDN 域名
88    pub cdn_domain: Option<String>,
89}
90
91/// AWS S3 配置
92#[derive(Debug, Clone, Serialize, Deserialize, Default)]
93pub struct AwsS3Config {
94    /// Access Key ID
95    pub access_key_id: String,
96    /// Secret Access Key
97    pub secret_access_key: String,
98    /// 存储桶名称
99    pub bucket: String,
100    /// 区域 (例如: us-east-1)
101    pub region: String,
102    /// 自定义 Endpoint (可选,用于兼容 S3 的服务如 MinIO)
103    pub endpoint: Option<String>,
104    /// CDN 域名
105    pub cdn_domain: Option<String>,
106}
107
108/// MinIO 配置
109#[derive(Debug, Clone, Serialize, Deserialize, Default)]
110pub struct MinioConfig {
111    /// Access Key
112    pub access_key: String,
113    /// Secret Key
114    pub secret_key: String,
115    /// 存储桶名称
116    pub bucket: String,
117    /// Endpoint (例如: http://localhost:9000)
118    pub endpoint: String,
119    /// 是否使用 SSL
120    pub use_ssl: bool,
121}
122
123/// 华为云 OBS 配置
124#[derive(Debug, Clone, Serialize, Deserialize, Default)]
125pub struct HuaweiObsConfig {
126    /// Access Key ID
127    pub access_key_id: String,
128    /// Secret Access Key
129    pub secret_access_key: String,
130    /// 存储桶名称
131    pub bucket: String,
132    /// 区域节点 (例如: cn-north-4)
133    pub endpoint: String,
134    /// CDN 域名
135    pub cdn_domain: Option<String>,
136}
137
138/// RustFS 本地文件系统配置
139#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct RustFsConfig {
141    /// 本地存储根目录路径
142    pub root_path: String,
143    /// 公共访问 URL 前缀
144    pub public_url_prefix: String,
145    /// 是否自动创建目录
146    pub auto_create_dir: bool,
147}
148
149impl Default for RustFsConfig {
150    fn default() -> Self {
151        Self {
152            root_path: "./uploads".to_string(),
153            public_url_prefix: "/uploads".to_string(),
154            auto_create_dir: true,
155        }
156    }
157}
158
159impl CosConfig {
160    /// 获取当前提供商的存储桶名称
161    pub fn get_bucket(&self) -> String {
162        match self.provider.as_str() {
163            "tencent" => self.tencent.bucket.clone(),
164            "aliyun" => self.aliyun.bucket.clone(),
165            "aws" => self.aws.bucket.clone(),
166            "minio" => self.minio.bucket.clone(),
167            "huawei" => self.huawei.bucket.clone(),
168            "rustfs" => String::new(),
169            _ => String::new(),
170        }
171    }
172
173    /// 获取当前提供商的 CDN 域名
174    pub fn get_cdn_domain(&self) -> Option<String> {
175        match self.provider.as_str() {
176            "tencent" => self.tencent.cdn_domain.clone(),
177            "aliyun" => self.aliyun.cdn_domain.clone(),
178            "aws" => self.aws.cdn_domain.clone(),
179            "huawei" => self.huawei.cdn_domain.clone(),
180            _ => None,
181        }
182    }
183
184    /// 获取当前提供商的 Endpoint
185    pub fn get_endpoint(&self) -> Option<String> {
186        match self.provider.as_str() {
187            "minio" => Some(self.minio.endpoint.clone()),
188            "huawei" => Some(self.huawei.endpoint.clone()),
189            "aliyun" => Some(self.aliyun.endpoint.clone()),
190            "aws" => self.aws.endpoint.clone(),
191            _ => None,
192        }
193    }
194
195    /// 获取本地文件系统的根路径(仅 RustFS)
196    pub fn get_root_path(&self) -> Option<String> {
197        match self.provider.as_str() {
198            "rustfs" => Some(self.rustfs.root_path.clone()),
199            _ => None,
200        }
201    }
202}