s3-wire 0.3.0

An async, streaming S3-compatible client for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Typed client configuration.

use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use http::HeaderValue;

use crate::credentials::{CredentialsProvider, EnvironmentCredentialsProvider};
use crate::endpoint::Endpoint;
use crate::error::S3Error;
use crate::observer::RequestObserver;
use crate::retry::RetryPolicy;

pub use crate::endpoint::AddressingStyle;

/// Validated configuration used to construct an S3 client.
#[derive(Clone)]
pub struct S3Config {
    endpoint: Endpoint,
    region: String,
    bucket: String,
    addressing_style: AddressingStyle,
    connect_timeout: Duration,
    attempt_timeout: Duration,
    operation_timeout: Duration,
    idle_body_timeout: Duration,
    max_xml_response_size: usize,
    max_error_response_size: usize,
    retry_policy: RetryPolicy,
    user_agent: String,
    credentials_provider: Arc<dyn CredentialsProvider>,
    observer: Option<Arc<dyn RequestObserver>>,
}

impl S3Config {
    /// Starts a configuration builder with HTTPS-safe defaults.
    pub fn builder() -> S3ConfigBuilder {
        S3ConfigBuilder::default()
    }

    /// Returns the service endpoint.
    pub fn endpoint(&self) -> &Endpoint {
        &self.endpoint
    }

    /// Returns the signing region.
    pub fn region(&self) -> &str {
        &self.region
    }

    /// Returns the configured bucket.
    pub fn bucket(&self) -> &str {
        &self.bucket
    }

    /// Returns the configured bucket addressing style.
    pub fn addressing_style(&self) -> AddressingStyle {
        self.addressing_style
    }

    /// Returns the connection-establishment timeout.
    pub fn connect_timeout(&self) -> Duration {
        self.connect_timeout
    }

    /// Returns the timeout applied to a single request attempt.
    pub fn attempt_timeout(&self) -> Duration {
        self.attempt_timeout
    }

    /// Returns the overall operation timeout across all attempts.
    pub fn operation_timeout(&self) -> Duration {
        self.operation_timeout
    }

    /// Returns the maximum permitted gap between response body chunks.
    pub fn idle_body_timeout(&self) -> Duration {
        self.idle_body_timeout
    }

    /// Returns the maximum XML response body size in bytes.
    pub fn max_xml_response_size(&self) -> usize {
        self.max_xml_response_size
    }

    /// Returns the maximum error response body size in bytes.
    pub fn max_error_response_size(&self) -> usize {
        self.max_error_response_size
    }

    /// Returns the retry policy.
    pub fn retry_policy(&self) -> &RetryPolicy {
        &self.retry_policy
    }

    /// Returns the HTTP user-agent value.
    pub fn user_agent(&self) -> &str {
        &self.user_agent
    }

    /// Returns the credential provider.
    pub fn credentials_provider(&self) -> &Arc<dyn CredentialsProvider> {
        &self.credentials_provider
    }

    /// Returns the optional sanitized request observer.
    pub fn observer(&self) -> Option<&Arc<dyn RequestObserver>> {
        self.observer.as_ref()
    }

    /// Clones this service configuration for another bucket.
    ///
    /// This is used by [`crate::S3Client::for_bucket`] to retain the same
    /// connection pool and credential provider while validating the new bucket
    /// for the configured addressing style.
    ///
    /// # Errors
    ///
    /// Returns an error when the bucket is empty or invalid for the configured
    /// endpoint and addressing style.
    pub fn for_bucket(&self, bucket: impl Into<String>) -> Result<Self, S3Error> {
        let bucket = bucket.into();
        if bucket.is_empty() {
            return Err(S3Error::configuration("bucket must not be empty"));
        }
        self.endpoint
            .object_url(&bucket, None, self.addressing_style)?;
        let mut config = self.clone();
        config.bucket = bucket;
        Ok(config)
    }
}

impl fmt::Debug for S3Config {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("S3Config")
            .field("endpoint", &self.endpoint)
            .field("region", &self.region)
            .field("bucket", &self.bucket)
            .field("addressing_style", &self.addressing_style)
            .field("connect_timeout", &self.connect_timeout)
            .field("attempt_timeout", &self.attempt_timeout)
            .field("operation_timeout", &self.operation_timeout)
            .field("idle_body_timeout", &self.idle_body_timeout)
            .field("max_xml_response_size", &self.max_xml_response_size)
            .field("max_error_response_size", &self.max_error_response_size)
            .field("retry_policy", &self.retry_policy)
            .field("user_agent", &self.user_agent)
            .field("credentials_provider", &"[REDACTED]")
            .field("observer", &self.observer.as_ref().map(|_| "configured"))
            .finish()
    }
}

/// Builder for [`S3Config`].
pub struct S3ConfigBuilder {
    endpoint: Endpoint,
    region: String,
    bucket: Option<String>,
    addressing_style: AddressingStyle,
    allow_http: bool,
    connect_timeout: Duration,
    attempt_timeout: Duration,
    operation_timeout: Duration,
    idle_body_timeout: Duration,
    max_xml_response_size: usize,
    max_error_response_size: usize,
    retry_policy: RetryPolicy,
    user_agent: String,
    credentials_provider: Arc<dyn CredentialsProvider>,
    observer: Option<Arc<dyn RequestObserver>>,
}

impl S3ConfigBuilder {
    /// Sets a custom endpoint. HTTP endpoints still require explicit opt-in.
    pub fn endpoint(mut self, endpoint: Endpoint) -> Self {
        self.endpoint = endpoint;
        self
    }

    /// Sets the SigV4 signing region.
    pub fn region(mut self, region: impl Into<String>) -> Self {
        self.region = region.into();
        self
    }

    /// Sets the target bucket.
    pub fn bucket(mut self, bucket: impl Into<String>) -> Self {
        self.bucket = Some(bucket.into());
        self
    }

    /// Sets the bucket addressing style.
    pub fn addressing_style(mut self, style: AddressingStyle) -> Self {
        self.addressing_style = style;
        self
    }

    /// Explicitly permits plain HTTP for local S3-compatible testing.
    ///
    /// This does not disable TLS verification for HTTPS endpoints and should not be
    /// enabled for endpoints reached over an untrusted network.
    pub fn allow_http_for_local_testing(mut self) -> Self {
        self.allow_http = true;
        self
    }

    /// Sets the connection-establishment timeout.
    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = timeout;
        self
    }

    /// Sets the timeout for each individual request attempt.
    pub fn attempt_timeout(mut self, timeout: Duration) -> Self {
        self.attempt_timeout = timeout;
        self
    }

    /// Sets the overall timeout for one primitive S3 operation across retries.
    ///
    /// Managed multipart uploads instead use the transfer and cleanup deadlines
    /// in [`crate::MultipartOptions`].
    pub fn operation_timeout(mut self, timeout: Duration) -> Self {
        self.operation_timeout = timeout;
        self
    }

    /// Sets the maximum idle period while streaming a response body.
    pub fn idle_body_timeout(mut self, timeout: Duration) -> Self {
        self.idle_body_timeout = timeout;
        self
    }

    /// Sets the maximum XML response body size.
    pub fn max_xml_response_size(mut self, bytes: usize) -> Self {
        self.max_xml_response_size = bytes;
        self
    }

    /// Sets the maximum error response body size.
    pub fn max_error_response_size(mut self, bytes: usize) -> Self {
        self.max_error_response_size = bytes;
        self
    }

    /// Sets the retry policy.
    pub fn retry_policy(mut self, policy: RetryPolicy) -> Self {
        self.retry_policy = policy;
        self
    }

    /// Sets the HTTP user-agent value.
    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = user_agent.into();
        self
    }

    /// Sets the asynchronous credential provider.
    pub fn credentials_provider(mut self, provider: Arc<dyn CredentialsProvider>) -> Self {
        self.credentials_provider = provider;
        self
    }

    /// Installs a dependency-free observer for sanitized request lifecycle events.
    pub fn observer(mut self, observer: Arc<dyn RequestObserver>) -> Self {
        self.observer = Some(observer);
        self
    }

    /// Validates and creates the configuration.
    pub fn build(self) -> Result<S3Config, S3Error> {
        let bucket = self
            .bucket
            .ok_or_else(|| S3Error::configuration("bucket is required"))?;
        if !self.endpoint.is_https() && !self.allow_http {
            return Err(S3Error::configuration(
                "plain HTTP requires allow_http_for_local_testing",
            ));
        }
        validate_nonempty_token("region", &self.region)?;
        if bucket.is_empty() {
            return Err(S3Error::configuration("bucket must not be empty"));
        }
        for (name, timeout) in [
            ("connect timeout", self.connect_timeout),
            ("attempt timeout", self.attempt_timeout),
            ("operation timeout", self.operation_timeout),
            ("idle body timeout", self.idle_body_timeout),
        ] {
            if timeout.is_zero() {
                return Err(S3Error::configuration(format!(
                    "{name} must be greater than zero"
                )));
            }
            if std::time::Instant::now().checked_add(timeout).is_none() {
                return Err(S3Error::configuration(format!(
                    "{name} is too large to represent as a deadline"
                )));
            }
        }
        if self.max_xml_response_size == 0 || self.max_error_response_size == 0 {
            return Err(S3Error::configuration(
                "response body limits must be greater than zero",
            ));
        }
        HeaderValue::from_str(&self.user_agent)
            .map_err(|_| S3Error::configuration("user agent is not a valid HTTP header value"))?;
        if self.user_agent.is_empty() {
            return Err(S3Error::configuration("user agent must not be empty"));
        }

        // Ensure this bucket is valid for the selected style before any signed request.
        self.endpoint
            .object_url(&bucket, None, self.addressing_style)?;

        Ok(S3Config {
            endpoint: self.endpoint,
            region: self.region,
            bucket,
            addressing_style: self.addressing_style,
            connect_timeout: self.connect_timeout,
            attempt_timeout: self.attempt_timeout,
            operation_timeout: self.operation_timeout,
            idle_body_timeout: self.idle_body_timeout,
            max_xml_response_size: self.max_xml_response_size,
            max_error_response_size: self.max_error_response_size,
            retry_policy: self.retry_policy,
            user_agent: self.user_agent,
            credentials_provider: self.credentials_provider,
            observer: self.observer,
        })
    }
}

impl Default for S3ConfigBuilder {
    fn default() -> Self {
        Self {
            endpoint: Endpoint::default(),
            region: "us-east-1".to_owned(),
            bucket: None,
            addressing_style: AddressingStyle::Path,
            allow_http: false,
            connect_timeout: Duration::from_secs(10),
            attempt_timeout: Duration::from_secs(30),
            operation_timeout: Duration::from_secs(5 * 60),
            idle_body_timeout: Duration::from_secs(30),
            max_xml_response_size: 1024 * 1024,
            max_error_response_size: 64 * 1024,
            retry_policy: RetryPolicy::default(),
            user_agent: format!("s3-wire/{}", env!("CARGO_PKG_VERSION")),
            credentials_provider: Arc::new(EnvironmentCredentialsProvider::new()),
            observer: None,
        }
    }
}

fn validate_nonempty_token(name: &str, value: &str) -> Result<(), S3Error> {
    if value.is_empty()
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_')
    {
        return Err(S3Error::configuration(format!("{name} is invalid")));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn https_is_the_default_and_http_requires_opt_in() {
        let default_config = S3Config::builder().bucket("bucket").build().unwrap();
        assert!(default_config.endpoint().is_https());

        let endpoint = Endpoint::new("http://127.0.0.1:9000").unwrap();
        assert!(
            S3Config::builder()
                .endpoint(endpoint.clone())
                .bucket("bucket")
                .build()
                .is_err()
        );
        assert!(
            S3Config::builder()
                .endpoint(endpoint)
                .allow_http_for_local_testing()
                .bucket("bucket")
                .build()
                .is_ok()
        );
    }

    #[test]
    fn timeouts_must_fit_in_an_instant_deadline() {
        assert!(
            S3Config::builder()
                .bucket("bucket")
                .operation_timeout(Duration::MAX)
                .build()
                .is_err()
        );
    }
}