mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-07 18:07:47 +08:00
Merge pull request #680 from Kayphoon/codex/s3-backup-user-agent
feat(admin): configure S3 backup User-Agent
This commit is contained in:
@@ -11,6 +11,7 @@ pub(crate) struct S3BackupConfig {
|
||||
pub(crate) scope: BackupScope,
|
||||
pub(crate) endpoint: String,
|
||||
pub(crate) region: String,
|
||||
pub(crate) user_agent: String,
|
||||
pub(crate) bucket: String,
|
||||
pub(crate) prefix: String,
|
||||
pub(crate) access_key_id: String,
|
||||
@@ -104,6 +105,8 @@ impl S3BackupConfig {
|
||||
endpoint,
|
||||
region: optional_string(entries, "backup_s3_region")?
|
||||
.unwrap_or_else(|| "auto".to_string()),
|
||||
user_agent: optional_string(entries, "backup_s3_user_agent")?
|
||||
.unwrap_or_else(|| "rclone/v1.68.0".to_string()),
|
||||
bucket,
|
||||
prefix: optional_string(entries, "backup_s3_prefix")?
|
||||
.unwrap_or_else(|| "aether/backups/".to_string()),
|
||||
@@ -179,6 +182,7 @@ fn config_label(key: &str) -> &str {
|
||||
match key {
|
||||
"backup_s3_endpoint" => "Endpoint(S3 地址)",
|
||||
"backup_s3_region" => "Region(S3 区域)",
|
||||
"backup_s3_user_agent" => "User-Agent(请求头标识)",
|
||||
"backup_s3_bucket" => "Bucket(存储桶)",
|
||||
"backup_s3_prefix" => "Prefix(备份前缀)",
|
||||
"backup_s3_access_key_id" => "Access Key ID(访问密钥 ID)",
|
||||
@@ -384,6 +388,7 @@ mod tests {
|
||||
|
||||
assert_eq!(config.scope, BackupScope::Data);
|
||||
assert_eq!(config.region, "auto");
|
||||
assert_eq!(config.user_agent, "rclone/v1.68.0");
|
||||
assert_eq!(config.prefix, "aether/backups/");
|
||||
assert_eq!(config.path_style, true);
|
||||
assert_eq!(config.compression, "zstd");
|
||||
|
||||
@@ -132,6 +132,7 @@ mod tests {
|
||||
scope,
|
||||
endpoint: "https://example.com".to_string(),
|
||||
region: "auto".to_string(),
|
||||
user_agent: "rclone/v1.68.0".to_string(),
|
||||
bucket: "aether-backups".to_string(),
|
||||
prefix: "prod/".to_string(),
|
||||
access_key_id: "test-access-key".to_string(),
|
||||
|
||||
@@ -6,7 +6,8 @@ use bytes::Bytes;
|
||||
use futures_util::TryStreamExt;
|
||||
use object_store::aws::AmazonS3Builder;
|
||||
use object_store::path::Path;
|
||||
use object_store::ObjectStore;
|
||||
use object_store::{ClientOptions, ObjectStore};
|
||||
use reqwest::header::HeaderValue;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use super::config::S3BackupConfig;
|
||||
@@ -84,7 +85,19 @@ pub(crate) struct ObjectStoreS3BackupStore {
|
||||
|
||||
impl ObjectStoreS3BackupStore {
|
||||
pub(crate) fn from_config(config: &S3BackupConfig) -> Result<Self, BackupStoreError> {
|
||||
// 部分 S3 兼容网关(如中国科技云 s3.cstcloud.cn)按 User-Agent 放行请求,
|
||||
// object_store 默认 UA 会被拒,因此允许自定义 User-Agent。
|
||||
let client_options = if config.user_agent.trim().is_empty() {
|
||||
ClientOptions::new()
|
||||
} else {
|
||||
ClientOptions::new().with_user_agent(
|
||||
HeaderValue::from_str(config.user_agent.trim()).map_err(|error| {
|
||||
BackupStoreError::new(format!("S3 备份 User-Agent 配置无效: {error}"))
|
||||
})?,
|
||||
)
|
||||
};
|
||||
let store = AmazonS3Builder::new()
|
||||
.with_client_options(client_options)
|
||||
.with_endpoint(config.endpoint.clone())
|
||||
.with_region(config.region.clone())
|
||||
.with_bucket_name(config.bucket.clone())
|
||||
|
||||
@@ -31,6 +31,7 @@ const S3_BACKUP_CONFIG_KEYS: &[&str] = &[
|
||||
"backup_s3_scope",
|
||||
"backup_s3_endpoint",
|
||||
"backup_s3_region",
|
||||
"backup_s3_user_agent",
|
||||
"backup_s3_bucket",
|
||||
"backup_s3_prefix",
|
||||
"backup_s3_access_key_id",
|
||||
|
||||
Reference in New Issue
Block a user