use std::fmt::Debug;
use opendal_core::Configurator;
use opendal_core::OperatorUri;
use opendal_core::Result;
use serde::Deserialize;
use serde::Serialize;
use crate::backend::S3Builder;
#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(default)]
#[non_exhaustive]
pub struct S3Config {
pub root: Option<String>,
#[serde(alias = "aws_bucket", alias = "aws_bucket_name", alias = "bucket_name")]
pub bucket: String,
pub enable_versioning: bool,
#[serde(
alias = "aws_endpoint",
alias = "aws_endpoint_url",
alias = "endpoint_url"
)]
pub endpoint: Option<String>,
#[serde(alias = "aws_region")]
pub region: Option<String>,
#[serde(alias = "aws_access_key_id")]
pub access_key_id: Option<String>,
#[serde(alias = "aws_secret_access_key")]
pub secret_access_key: Option<String>,
#[serde(alias = "aws_session_token", alias = "aws_token", alias = "token")]
pub session_token: Option<String>,
pub role_arn: Option<String>,
pub external_id: Option<String>,
pub role_session_name: Option<String>,
pub disable_config_load: bool,
pub disable_ec2_metadata: bool,
pub allow_anonymous: bool,
#[serde(alias = "aws_server_side_encryption")]
pub server_side_encryption: Option<String>,
#[serde(alias = "aws_sse_kms_key_id")]
pub server_side_encryption_aws_kms_key_id: Option<String>,
pub server_side_encryption_customer_algorithm: Option<String>,
#[serde(alias = "aws_sse_customer_key_base64")]
pub server_side_encryption_customer_key: Option<String>,
pub server_side_encryption_customer_key_md5: Option<String>,
pub default_storage_class: Option<String>,
#[serde(
alias = "aws_virtual_hosted_style_request",
alias = "virtual_hosted_style_request"
)]
pub enable_virtual_host_style: bool,
#[deprecated(
since = "0.52.0",
note = "Please use `delete_max_size` instead of `batch_max_operations`"
)]
pub batch_max_operations: Option<usize>,
pub delete_max_size: Option<usize>,
pub disable_stat_with_override: bool,
#[serde(alias = "aws_checksum_algorithm")]
pub checksum_algorithm: Option<String>,
pub disable_write_with_if_match: bool,
pub enable_write_with_append: bool,
pub disable_list_objects_v2: bool,
#[serde(alias = "aws_request_payer", alias = "request_payer")]
pub enable_request_payer: bool,
pub default_acl: Option<String>,
}
impl Debug for S3Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("S3Config")
.field("root", &self.root)
.field("bucket", &self.bucket)
.field("endpoint", &self.endpoint)
.field("region", &self.region)
.finish_non_exhaustive()
}
}
impl Configurator for S3Config {
type Builder = S3Builder;
fn from_uri(uri: &OperatorUri) -> Result<Self> {
let mut map = uri.options().clone();
if let Some(name) = uri.name() {
map.insert("bucket".to_string(), name.to_string());
}
if let Some(root) = uri.root() {
map.insert("root".to_string(), root.to_string());
}
Self::from_iter(map)
}
#[allow(deprecated)]
fn into_builder(self) -> Self::Builder {
S3Builder {
config: self,
credential_providers: None,
}
}
}
#[cfg(test)]
mod tests {
use std::iter;
use super::*;
use opendal_core::Configurator;
use opendal_core::OperatorUri;
#[test]
fn test_s3_config_original_field_names() {
let json = r#"{
"bucket": "test-bucket",
"access_key_id": "test-key",
"secret_access_key": "test-secret",
"region": "us-west-2",
"endpoint": "https://s3.amazonaws.com",
"session_token": "test-token"
}"#;
let config: S3Config = serde_json::from_str(json).unwrap();
assert_eq!(config.bucket, "test-bucket");
assert_eq!(config.access_key_id, Some("test-key".to_string()));
assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
assert_eq!(config.region, Some("us-west-2".to_string()));
assert_eq!(
config.endpoint,
Some("https://s3.amazonaws.com".to_string())
);
assert_eq!(config.session_token, Some("test-token".to_string()));
}
#[test]
fn test_s3_config_aws_prefixed_aliases() {
let json = r#"{
"aws_bucket": "test-bucket",
"aws_access_key_id": "test-key",
"aws_secret_access_key": "test-secret",
"aws_region": "us-west-2",
"aws_endpoint": "https://s3.amazonaws.com",
"aws_session_token": "test-token"
}"#;
let config: S3Config = serde_json::from_str(json).unwrap();
assert_eq!(config.bucket, "test-bucket");
assert_eq!(config.access_key_id, Some("test-key".to_string()));
assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
assert_eq!(config.region, Some("us-west-2".to_string()));
assert_eq!(
config.endpoint,
Some("https://s3.amazonaws.com".to_string())
);
assert_eq!(config.session_token, Some("test-token".to_string()));
}
#[test]
fn test_s3_config_additional_aliases() {
let json = r#"{
"bucket_name": "test-bucket",
"token": "test-token",
"endpoint_url": "https://s3.amazonaws.com",
"virtual_hosted_style_request": true,
"aws_checksum_algorithm": "crc32c",
"request_payer": true
}"#;
let config: S3Config = serde_json::from_str(json).unwrap();
assert_eq!(config.bucket, "test-bucket");
assert_eq!(config.session_token, Some("test-token".to_string()));
assert_eq!(
config.endpoint,
Some("https://s3.amazonaws.com".to_string())
);
assert!(config.enable_virtual_host_style);
assert_eq!(config.checksum_algorithm, Some("crc32c".to_string()));
assert!(config.enable_request_payer);
}
#[test]
fn test_s3_config_encryption_aliases() {
let json = r#"{
"bucket": "test-bucket",
"aws_server_side_encryption": "aws:kms",
"aws_sse_kms_key_id": "test-kms-key",
"aws_sse_customer_key_base64": "dGVzdC1jdXN0b21lci1rZXk="
}"#;
let config: S3Config = serde_json::from_str(json).unwrap();
assert_eq!(config.bucket, "test-bucket");
assert_eq!(config.server_side_encryption, Some("aws:kms".to_string()));
assert_eq!(
config.server_side_encryption_aws_kms_key_id,
Some("test-kms-key".to_string())
);
assert_eq!(
config.server_side_encryption_customer_key,
Some("dGVzdC1jdXN0b21lci1rZXk=".to_string())
);
}
#[test]
fn from_uri_extracts_bucket_and_root() {
let uri = OperatorUri::new("s3://example-bucket/path/to/root", iter::empty()).unwrap();
let cfg = S3Config::from_uri(&uri).unwrap();
assert_eq!(cfg.bucket, "example-bucket");
assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
}
#[test]
fn from_uri_extracts_endpoint() {
let uri = OperatorUri::new(
"s3://example-bucket/path/to/root?endpoint=https%3A%2F%2Fcustom-s3-endpoint.com",
iter::empty(),
)
.unwrap();
let cfg = S3Config::from_uri(&uri).unwrap();
assert_eq!(cfg.bucket, "example-bucket");
assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
assert_eq!(
cfg.endpoint.as_deref(),
Some("https://custom-s3-endpoint.com")
);
}
}