1use aws_smithy_types::error::operation::BuildError;
7use std::cmp;
8
9use crate::types::ByteSize;
10
11mod runtime;
12pub use runtime::{
13 ConcurrencyMode, Managed, MemoryBudgetConfig, MemoryConfig, RuntimeConfig, TargetThroughput,
14};
15
16mod diagnostics;
17pub(crate) use diagnostics::{
18 DiagnosticsConfig, MemoryDiagnosticsConfig, TransferDiagnosticsConfig,
19};
20
21mod loader;
22pub use loader::ConfigLoader;
23pub(crate) mod user_agent;
24
25pub(crate) const MIN_MULTIPART_PART_SIZE: ByteSize = ByteSize::mib(5);
27
28#[non_exhaustive]
30#[derive(Debug, Clone, Default)]
31pub enum PartSize {
32 #[default]
34 Auto,
35
36 Target(ByteSize),
41}
42
43#[non_exhaustive]
52#[derive(Debug, Clone, PartialEq, Eq, Default)]
53pub enum ReadAhead {
54 #[default]
57 Auto,
58
59 Parts(usize),
66}
67
68pub struct S3ClientConfig {
91 pub(crate) config: aws_sdk_s3::Config,
92}
93
94impl S3ClientConfig {
95 pub fn new(config: impl Into<Self>) -> Self {
99 config.into()
100 }
101
102 pub(crate) fn from_sdk_builder(builder: aws_sdk_s3::config::Builder) -> Self {
103 Self {
104 config: builder.build(),
105 }
106 }
107}
108
109impl From<&aws_types::SdkConfig> for S3ClientConfig {
110 fn from(sdk_config: &aws_types::SdkConfig) -> Self {
111 Self::from_sdk_builder(aws_sdk_s3::config::Builder::from(sdk_config))
112 }
113}
114
115#[cfg(feature = "sdk-v1")]
116impl From<aws_sdk_s3::config::Builder> for S3ClientConfig {
117 fn from(builder: aws_sdk_s3::config::Builder) -> Self {
118 Self::from_sdk_builder(builder)
119 }
120}
121
122impl std::fmt::Debug for S3ClientConfig {
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 f.debug_struct("S3ClientConfig").finish_non_exhaustive()
125 }
126}
127
128#[derive(Debug)]
130pub(crate) enum S3ClientSource {
131 Provided(aws_sdk_s3::Client),
133 FromConfig(Box<S3ClientConfig>),
135}
136
137#[derive(Debug)]
139pub struct Config {
140 multipart_threshold: PartSize,
141 target_part_size: PartSize,
142 concurrency: ConcurrencyMode,
143 runtime_config: RuntimeConfig,
144 read_ahead: ReadAhead,
145 memory: MemoryConfig,
146 diagnostics: DiagnosticsConfig,
147 s3_client_source: Option<S3ClientSource>,
148 machine_profile: Option<crate::runtime::platform::MachineProfile>,
152 pub(crate) network_interfaces: Vec<String>,
155}
156
157impl Config {
158 pub fn builder() -> Builder {
164 Builder::default()
165 }
166
167 pub fn multipart_threshold(&self) -> &PartSize {
169 &self.multipart_threshold
170 }
171
172 pub fn part_size(&self) -> &PartSize {
174 &self.target_part_size
175 }
176
177 pub fn concurrency(&self) -> &ConcurrencyMode {
181 &self.concurrency
182 }
183
184 pub(crate) fn throughput_target_gbps(&self) -> Option<f64> {
186 match &self.concurrency {
187 ConcurrencyMode::TargetThroughput(target) => Some(target.as_gbps()),
188 _ => None,
189 }
190 }
191
192 pub fn runtime_config(&self) -> &RuntimeConfig {
194 &self.runtime_config
195 }
196
197 #[cfg(not(target_family = "wasm"))]
198 pub(crate) fn managed_config_mut(&mut self) -> Option<&mut Managed> {
199 self.runtime_config.managed_config_mut()
200 }
201
202 pub fn read_ahead(&self) -> &ReadAhead {
207 &self.read_ahead
208 }
209
210 pub fn memory(&self) -> &MemoryConfig {
212 &self.memory
213 }
214
215 pub(crate) fn diagnostics(&self) -> DiagnosticsConfig {
217 self.diagnostics
218 }
219
220 pub(crate) fn machine_profile(&self) -> Option<&crate::runtime::platform::MachineProfile> {
223 self.machine_profile.as_ref()
224 }
225
226 pub(crate) fn runtime_http(
231 &self,
232 max_connections_per_host: usize,
233 ) -> Option<crate::runtime::RuntimeHttpOptions> {
234 if cfg!(target_family = "wasm") {
235 return None;
236 }
237 match self.s3_client_source.as_ref()? {
238 S3ClientSource::FromConfig(config)
239 if self.runtime_config.managed_config().is_some()
240 && config.config.http_client().is_none() =>
241 {
242 Some(crate::runtime::RuntimeHttpOptions {
243 network_interfaces: self.network_interfaces.clone(),
244 max_connections_per_host,
245 })
246 }
247 _ => None,
248 }
249 }
250
251 pub(crate) fn take_s3_client_source(&mut self) -> S3ClientSource {
253 self.s3_client_source
254 .take()
255 .expect("s3 client source already taken")
256 }
257}
258
259#[derive(Debug, Default)]
261pub struct Builder {
262 multipart_threshold_part_size: PartSize,
263 target_part_size: PartSize,
264 concurrency: ConcurrencyMode,
265 runtime_config: RuntimeConfig,
266 read_ahead: ReadAhead,
267 memory: MemoryConfig,
268 diagnostics: Option<DiagnosticsConfig>,
269 client: Option<aws_sdk_s3::Client>,
270 s3_client_config: Option<S3ClientConfig>,
271 machine_profile: Option<crate::runtime::platform::MachineProfile>,
272}
273
274impl Builder {
275 pub fn multipart_threshold(self, threshold: PartSize) -> Self {
280 let threshold = match threshold {
281 PartSize::Target(part_size) => {
282 PartSize::Target(cmp::max(part_size, MIN_MULTIPART_PART_SIZE))
283 }
284 tps => tps,
285 };
286
287 self.set_multipart_threshold(threshold)
288 }
289
290 pub fn part_size(self, part_size: PartSize) -> Self {
303 let threshold = match part_size {
304 PartSize::Target(part_size) => {
305 PartSize::Target(cmp::max(part_size, MIN_MULTIPART_PART_SIZE))
306 }
307 tps => tps,
308 };
309
310 self.set_target_part_size(threshold)
311 }
312
313 pub(crate) fn set_multipart_threshold(mut self, threshold: PartSize) -> Self {
317 self.multipart_threshold_part_size = threshold;
318 self
319 }
320
321 pub(crate) fn set_target_part_size(mut self, threshold: PartSize) -> Self {
325 self.target_part_size = threshold;
326 self
327 }
328
329 pub fn concurrency(mut self, mode: ConcurrencyMode) -> Self {
334 self.concurrency = mode;
335 self
336 }
337
338 pub fn runtime_config(mut self, config: RuntimeConfig) -> Self {
342 self.set_runtime_config(Some(config));
343 self
344 }
345
346 pub fn set_runtime_config(&mut self, config: Option<RuntimeConfig>) -> &mut Self {
348 self.runtime_config = config.unwrap_or_default();
349 self
350 }
351
352 pub fn read_ahead(mut self, mode: ReadAhead) -> Self {
360 self.read_ahead = mode;
361 self
362 }
363
364 pub fn memory(mut self, memory: MemoryConfig) -> Self {
368 self.memory = memory;
369 self
370 }
371
372 #[cfg(test)]
374 pub(crate) fn diagnostics_for_test(
375 mut self,
376 memory: MemoryDiagnosticsConfig,
377 transfer_detail_level: u64,
378 ) -> Self {
379 self.diagnostics = Some(DiagnosticsConfig::for_test(memory, transfer_detail_level));
380 self
381 }
382
383 #[cfg(test)]
385 pub(crate) fn sdk_client(mut self, client: aws_sdk_s3::Client) -> Self {
386 self.client = Some(client);
387 self
388 }
389
390 pub fn s3_config(mut self, config: impl Into<S3ClientConfig>) -> Self {
398 self.s3_client_config = Some(config.into());
399 self
400 }
401
402 pub(crate) fn machine_profile(
405 mut self,
406 profile: Option<crate::runtime::platform::MachineProfile>,
407 ) -> Self {
408 self.machine_profile = profile;
409 self
410 }
411
412 pub fn build(self) -> Result<Config, BuildError> {
418 let s3_client_source = match (self.client, self.s3_client_config) {
419 (Some(client), _) => S3ClientSource::Provided(client),
420 (None, Some(config)) => S3ClientSource::FromConfig(Box::new(config)),
421 (None, None) => {
422 return Err(BuildError::missing_field(
423 "s3_config",
424 "S3 client configuration is required",
425 ))
426 }
427 };
428 Ok(Config {
429 multipart_threshold: self.multipart_threshold_part_size,
430 target_part_size: self.target_part_size,
431 concurrency: self.concurrency,
432 runtime_config: self.runtime_config,
433 read_ahead: self.read_ahead,
434 memory: self.memory,
435 diagnostics: self.diagnostics.unwrap_or_else(DiagnosticsConfig::from_env),
436 s3_client_source: Some(s3_client_source),
437 machine_profile: self.machine_profile,
438 network_interfaces: Vec::new(),
439 })
440 }
441}
442
443#[cfg(test)]
444mod tests {
445 use super::*;
446
447 fn s3_config_builder() -> aws_sdk_s3::config::Builder {
448 aws_sdk_s3::config::Builder::new()
449 }
450
451 #[test]
452 fn s3_client_config_converts_from_sdk_config_and_builder() {
453 let region = aws_types::region::Region::from_static("us-west-2");
454 let sdk_config = aws_types::SdkConfig::builder()
455 .region(region.clone())
456 .build();
457 let from_sdk = S3ClientConfig::from(&sdk_config);
458 assert_eq!(from_sdk.config.region(), Some(®ion));
459
460 let builder = s3_config_builder().region(region.clone());
461 #[cfg(feature = "sdk-v1")]
462 let from_builder = S3ClientConfig::new(builder);
463 #[cfg(not(feature = "sdk-v1"))]
464 let from_builder = S3ClientConfig::from_sdk_builder(builder);
465 assert_eq!(from_builder.config.region(), Some(®ion));
466 }
467
468 #[test]
469 fn runtime_http_present_for_s3_config() {
470 let config = Config::builder()
471 .s3_config(S3ClientConfig::from_sdk_builder(s3_config_builder()))
472 .build()
473 .expect("valid config");
474 let http = config.runtime_http(64).expect("runtime HTTP enabled");
475 assert!(http.network_interfaces.is_empty());
476 assert_eq!(http.max_connections_per_host, 64);
477 }
478
479 #[test]
480 fn runtime_http_absent_without_runtime_transport() {
481 let shared = Config::builder()
482 .s3_config(S3ClientConfig::from_sdk_builder(s3_config_builder()))
483 .runtime_config(RuntimeConfig::multi_thread_tokio())
484 .build()
485 .expect("valid config");
486 assert!(shared.runtime_http(64).is_none());
487
488 let configured = Config::builder()
489 .s3_config(S3ClientConfig::from_sdk_builder(
490 aws_smithy_mocks::mock_client!(aws_sdk_s3, [])
491 .config()
492 .to_builder(),
493 ))
494 .build()
495 .expect("valid config");
496 assert!(configured.runtime_http(64).is_none());
497
498 let provided = Config::builder()
501 .sdk_client(aws_smithy_mocks::mock_client!(aws_sdk_s3, []))
502 .build()
503 .expect("valid config");
504 assert!(provided.runtime_http(64).is_none());
505 }
506
507 #[test]
508 fn build_without_s3_config_is_an_error() {
509 let error = Config::builder()
510 .build()
511 .expect_err("s3_config is required");
512 assert!(
513 error.to_string().contains("s3_config"),
514 "error does not name the missing field: {error}"
515 );
516 }
517}