s3z 0.1.0-rc.2

S3 ops, but fearlessly fast!
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! Batch download operation — S3 prefix to local directory.

use std::{
    fmt,
    path::{Path, PathBuf},
    sync::Arc,
};

use tokio::sync::mpsc;

use crate::{
    client::S3Client,
    config::TransferConfig,
    error::{Error, Result},
    http::ObjectKey,
    ops::list::{ListRequest, ObjectInfo},
    trace::{maybe_debug, maybe_info},
    transfer::{download, pool, scheduler},
};

/// Default number of files downloaded in parallel.
const DEFAULT_WORKERS: usize = 32;
/// Default number of parts downloaded concurrently per file.
const DEFAULT_CONCURRENCY: usize = 8;

/// Total concurrent HTTP request budget used by [`tune_parallelism`].
const TOTAL_REQUEST_BUDGET: usize = 256;

/// Callback invoked after each file download completes successfully.
type ProgressCallback = Arc<dyn Fn(&FileDownloadResult) + Send + Sync>;

/// Outcome for a single downloaded file.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct FileDownloadResult {
    /// Local path the file was written to.
    pub dest: PathBuf,
    /// S3 key that was downloaded.
    pub key: String,
    /// Number of parts used (1 = single GET).
    pub parts: u32,
    /// File size in bytes.
    pub size: u64,
}

/// A batch download request.
///
/// Downloads all objects under `prefix` in `bucket` into `dest_dir`,
/// preserving the key structure relative to the prefix.
#[non_exhaustive]
pub struct DownloadRequest {
    /// Source S3 bucket.
    pub bucket: String,
    /// Number of parts downloaded concurrently within a single multipart download.
    pub concurrency_per_file: usize,
    /// Local directory to write files into.
    pub dest_dir: PathBuf,
    /// Callback invoked after each file download completes.
    on_file_complete: Option<ProgressCallback>,
    /// Key prefix in the bucket.
    pub prefix: String,
    /// Number of files downloaded in parallel.
    pub workers: usize,
}

impl fmt::Debug for DownloadRequest {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DownloadRequest")
            .field("bucket", &self.bucket)
            .field("concurrency_per_file", &self.concurrency_per_file)
            .field("dest_dir", &self.dest_dir)
            .field("on_file_complete", &self.on_file_complete.as_ref().map(|_| ".."))
            .field("prefix", &self.prefix)
            .field("workers", &self.workers)
            .finish()
    }
}

impl DownloadRequest {
    /// Create a new download request with sensible defaults.
    #[inline]
    #[must_use]
    pub fn new(
        bucket: impl Into<String>, prefix: impl Into<String>, dest_dir: impl Into<PathBuf>,
    ) -> Self {
        Self {
            bucket: bucket.into(),
            concurrency_per_file: DEFAULT_CONCURRENCY,
            dest_dir: dest_dir.into(),
            on_file_complete: None,
            prefix: prefix.into(),
            workers: DEFAULT_WORKERS,
        }
    }

    /// Auto-tune workers and concurrency based on the object list.
    ///
    /// Call this after listing when using [`S3Client::download_objects`]
    /// to let the heuristic pick optimal parallelism for the workload.
    #[inline]
    #[must_use]
    pub fn auto_tune(mut self, objects: &[ObjectInfo], multipart_threshold: u64) -> Self {
        let config = tune_parallelism(objects, multipart_threshold);
        self.workers = config.workers;
        self.concurrency_per_file = config.concurrency_per_file;
        self
    }

    /// Set a callback that fires after each file download completes.
    #[inline]
    #[must_use]
    pub fn on_file_complete(
        mut self, callback: impl Fn(&FileDownloadResult) + Send + Sync + 'static,
    ) -> Self {
        self.on_file_complete = Some(Arc::new(callback));
        self
    }
}

/// Result of a batch download.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DownloadResult {
    /// Per-file outcomes.
    pub files: Vec<FileDownloadResult>,
}

#[expect(clippy::multiple_inherent_impl, reason = "ops extend S3Client from their own modules")]
impl S3Client {
    /// Download all objects under a prefix to a local directory.
    ///
    /// Listing and downloading are pipelined: the first page of
    /// `ListObjectsV2` results is used to auto-tune parallelism, then
    /// remaining pages are fetched concurrently with downloads so that
    /// large listings don't block the start of data transfer.
    ///
    /// If `workers` or `concurrency_per_file` were left at their defaults,
    /// they are replaced with auto-tuned values. Explicitly set values are
    /// respected, but a tracing warning is emitted when they differ from
    /// the recommendation.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use s3z::{Config, S3Client, DownloadRequest, auth::CredentialSource};
    /// # async fn example() -> s3z::error::Result<()> {
    /// let client = S3Client::new(Config::new("us-east-1", CredentialSource::Env)).await?;
    /// let result = client.download(DownloadRequest::new("my-bucket", "data/", "./local/")).await?;
    /// for f in &result.files {
    ///     println!("{} -> {}", f.key, f.dest.display());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if listing, any individual download, or file I/O fails.
    pub async fn download(&self, mut req: DownloadRequest) -> Result<DownloadResult> {
        let mut paginator = self.list(ListRequest::new(&req.bucket, &req.prefix));

        // Fetch the first page to sample the workload for auto-tuning.
        let first_page = paginator.next_page().await?;
        let first_objects = match first_page {
            Some(page) => page.objects,
            None => {
                return Ok(DownloadResult {
                    files: Vec::new(),
                });
            },
        };

        // Auto-tune based on the first page sample.
        let tuned =
            tune_parallelism(&first_objects, self.config.transfer.multipart_download_threshold);
        let user_set_workers = req.workers != DEFAULT_WORKERS;
        let user_set_concurrency = req.concurrency_per_file != DEFAULT_CONCURRENCY;

        if user_set_workers || user_set_concurrency {
            // Respect explicit values, but warn if they differ from the recommendation.
            if req.workers != tuned.workers
                || req.concurrency_per_file != tuned.concurrency_per_file
            {
                maybe_info!(
                    user_workers = req.workers,
                    user_concurrency = req.concurrency_per_file,
                    recommended_workers = tuned.workers,
                    recommended_concurrency = tuned.concurrency_per_file,
                    "using user-specified parallelism; auto-tuned values differ"
                );
            }
        } else {
            req.workers = tuned.workers;
            req.concurrency_per_file = tuned.concurrency_per_file;
        }

        // Buffer enough items to keep the pool fed without over-allocating.
        let channel_cap = req.workers.saturating_mul(2).max(1);
        let (tx, rx) = mpsc::channel::<ObjectInfo>(channel_cap);

        // Spawn the paginator as a background producer. It sends the first
        // page's objects, then continues fetching remaining pages.
        // Returns Ok(()) on success, Err on listing failure.
        let mut paginator_owned = paginator;
        let list_handle = tokio::task::spawn(async move {
            // Send first-page objects.
            for obj in first_objects {
                if tx.send(obj).await.is_err() {
                    return Ok(());
                }
            }
            // Continue with remaining pages.
            loop {
                match paginator_owned.next_page().await {
                    Ok(Some(page)) => {
                        for obj in page.objects {
                            if tx.send(obj).await.is_err() {
                                return Ok(());
                            }
                        }
                    },
                    Ok(None) => return Ok(()),
                    Err(e) => return Err(e),
                }
            }
        });

        let http = self.http.clone();
        let config = self.config.clone();
        let creds = self.creds.clone();
        let bucket = req.bucket.clone();
        let prefix = req.prefix.clone();
        let dest_dir = req.dest_dir.clone();
        let concurrency = req.concurrency_per_file;

        let on_complete: Option<&(dyn Fn(&FileDownloadResult) + Send + Sync)> =
            req.on_file_complete.as_deref();

        let pool_result = pool::run_pool_rx(
            rx,
            req.workers,
            |obj| {
                let http = http.clone();
                let cfg = config.clone();
                let cr = creds.clone();
                let bkt = bucket.clone();
                let pfx = prefix.clone();
                let dest = dest_dir.clone();
                async move {
                    download_single_object(&http, &cfg, &cr, &bkt, &pfx, &obj, &dest, concurrency)
                        .await
                }
            },
            on_complete,
        )
        .await;

        // Check the listing task first — a listing failure is the root cause
        // when the pool starves because the channel closed early.
        let list_result = list_handle.await.map_err(|e| Error::Internal(e.to_string()))?;
        list_result?;

        let files = pool_result?;

        Ok(DownloadResult {
            files,
        })
    }

    /// Download a pre-listed set of objects to a local directory.
    ///
    /// Use this when you have already listed the objects (e.g. to build a
    /// progress bar) and want to avoid a second `ListObjectsV2` round-trip.
    ///
    /// # Errors
    ///
    /// Returns an error if any individual download or file I/O fails.
    pub async fn download_objects(
        &self, req: DownloadRequest, objects: Vec<ObjectInfo>,
    ) -> Result<DownloadResult> {
        #[cfg(feature = "tracing")]
        let total = objects.len();
        maybe_info!(
            files = total,
            workers = req.workers,
            concurrency = req.concurrency_per_file,
            bucket = %req.bucket,
            prefix = %req.prefix,
            dest = %req.dest_dir.display(),
            "starting download batch"
        );

        if objects.is_empty() {
            return Ok(DownloadResult {
                files: Vec::new(),
            });
        }

        let http = self.http.clone();
        let config = self.config.clone();
        let creds = self.creds.clone();
        let bucket = req.bucket.clone();
        let prefix = req.prefix.clone();
        let dest_dir = req.dest_dir.clone();
        let concurrency = req.concurrency_per_file;

        let on_complete: Option<&(dyn Fn(&FileDownloadResult) + Send + Sync)> =
            req.on_file_complete.as_deref();

        let files = pool::run_pool(
            objects,
            req.workers,
            |obj| {
                let http = http.clone();
                let cfg = config.clone();
                let cr = creds.clone();
                let bkt = bucket.clone();
                let pfx = prefix.clone();
                let dest = dest_dir.clone();
                async move {
                    download_single_object(&http, &cfg, &cr, &bkt, &pfx, &obj, &dest, concurrency)
                        .await
                }
            },
            on_complete,
        )
        .await?;

        Ok(DownloadResult {
            files,
        })
    }
}

/// Parallelism settings returned by [`tune_parallelism`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParallelismConfig {
    /// Number of files downloaded in parallel.
    pub workers: usize,
    /// Number of parts downloaded concurrently within a single multipart download.
    pub concurrency_per_file: usize,
}

/// Analyze a set of objects and return optimal `(workers, concurrency_per_file)`.
///
/// Uses a fixed total request budget (256 concurrent requests) and distributes
/// it based on the median file size relative to the multipart threshold:
///
/// - **Small-files workload** (median ≤ threshold): most files will use a single GET, so maximize
///   worker count and set concurrency to 1.
/// - **Large-files workload** (median > threshold): fewer workers, more per-file concurrency to
///   saturate bandwidth on each large object.
///
/// Returns defaults if the object list is empty.
#[must_use]
pub fn tune_parallelism(objects: &[ObjectInfo], multipart_threshold: u64) -> ParallelismConfig {
    if objects.is_empty() {
        return ParallelismConfig {
            workers: DEFAULT_WORKERS,
            concurrency_per_file: DEFAULT_CONCURRENCY,
        };
    }

    let mut sizes: Vec<u64> = objects.iter().map(|o| o.size).collect();
    let mid = sizes.len() / 2;
    sizes.select_nth_unstable(mid);
    let median = sizes[mid];

    if median <= multipart_threshold {
        // Small-files mode: single GET per file, maximize parallelism.
        let workers = TOTAL_REQUEST_BUDGET.min(objects.len());
        ParallelismConfig {
            workers: workers.max(1),
            concurrency_per_file: 1,
        }
    } else {
        // Large-files mode: fewer workers, more parts per file.
        // Target matches DEFAULT_CONCURRENCY; cap so
        // workers * concurrency ≤ budget.
        let target_concurrency = DEFAULT_CONCURRENCY;
        let workers = (TOTAL_REQUEST_BUDGET / target_concurrency).min(objects.len()).max(1);
        ParallelismConfig {
            workers,
            concurrency_per_file: target_concurrency,
        }
    }
}

/// Download a single object — picks single GET or multipart based on size.
///
/// Concurrency is dynamically scaled per file via
/// [`scheduler::concurrency_for_size`]: small files that exceed the
/// multipart threshold get fewer concurrent Range requests, large files
/// get more to saturate bandwidth.
#[expect(clippy::too_many_arguments, reason = "internal fn, context struct would add indirection")]
async fn download_single_object(
    http: &reqwest::Client, config: &crate::config::Config, creds: &crate::auth::Credentials,
    bucket: &str, prefix: &str, obj: &ObjectInfo, dest_dir: &Path, _concurrency: usize,
) -> Result<FileDownloadResult> {
    let rel_key = obj.key.strip_prefix(prefix).unwrap_or(&obj.key);
    let dest = dest_dir.join(rel_key);
    let key = ObjectKey::new(&obj.key);

    #[cfg(feature = "tracing")]
    let file_start = std::time::Instant::now();

    let (size, parts) = if obj.size <= config.transfer.multipart_download_threshold {
        maybe_debug!(key = %key, size = obj.size, "single GET");
        let size = download::download_single(http, config, creds, bucket, &key, &dest).await?;
        (size, 1_u32)
    } else {
        let concurrency = scheduler::concurrency_for_size(obj.size);
        maybe_debug!(key = %key, size = obj.size, concurrency, "multipart download");
        let part_size = scheduler::compute_download_part_size(obj.size, concurrency);
        let transfer = TransferConfig {
            part_size,
            ..config.transfer
        };
        let parts_plan = scheduler::plan_parts(obj.size, &transfer);
        let parts_count =
            u32::try_from(parts_plan.len()).map_err(|e| Error::Conversion(e.to_string()))?;
        let size = download::download_multipart(
            http,
            config,
            creds,
            bucket,
            &key,
            &parts_plan,
            &dest,
            obj.size,
            concurrency,
        )
        .await?;
        (size, parts_count)
    };

    #[cfg(feature = "tracing")]
    let elapsed = file_start.elapsed();

    maybe_info!(key = %key, size, parts, ?elapsed, "file downloaded");
    Ok(FileDownloadResult {
        dest,
        key: obj.key.clone(),
        parts,
        size,
    })
}

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

    const MB: u64 = 1024 * 1024;
    const THRESHOLD: u64 = 50 * MB;

    fn make_objects(sizes: &[u64]) -> Vec<ObjectInfo> {
        sizes
            .iter()
            .enumerate()
            .map(|(i, &size)| {
                ObjectInfo {
                    key: format!("file{i}.bin"),
                    size,
                    etag: String::new(),
                    last_modified: String::new(),
                }
            })
            .collect()
    }

    #[test]
    fn tune_empty_returns_defaults() {
        let cfg = tune_parallelism(&[], THRESHOLD);
        assert_eq!(cfg.workers, DEFAULT_WORKERS);
        assert_eq!(cfg.concurrency_per_file, DEFAULT_CONCURRENCY);
    }

    #[test]
    fn tune_small_files_maximizes_workers() {
        let objects = make_objects(&[MB; 100]);
        let cfg = tune_parallelism(&objects, THRESHOLD);
        assert_eq!(cfg.workers, 100);
        assert_eq!(cfg.concurrency_per_file, 1);
    }

    #[test]
    fn tune_small_files_caps_at_budget() {
        let objects = make_objects(&[MB; 1000]);
        let cfg = tune_parallelism(&objects, THRESHOLD);
        assert_eq!(cfg.workers, TOTAL_REQUEST_BUDGET);
        assert_eq!(cfg.concurrency_per_file, 1);
    }

    #[test]
    fn tune_large_files_uses_concurrency() {
        let objects = make_objects(&[500 * MB; 10]);
        let cfg = tune_parallelism(&objects, THRESHOLD);
        assert_eq!(cfg.concurrency_per_file, DEFAULT_CONCURRENCY);
        assert_eq!(cfg.workers, 10);
    }

    #[test]
    fn tune_large_files_caps_workers() {
        let objects = make_objects(&[500 * MB; 100]);
        let cfg = tune_parallelism(&objects, THRESHOLD);
        assert_eq!(cfg.concurrency_per_file, DEFAULT_CONCURRENCY);
        assert_eq!(cfg.workers, TOTAL_REQUEST_BUDGET / DEFAULT_CONCURRENCY);
    }

    #[test]
    fn tune_single_small_file() {
        let objects = make_objects(&[10 * MB]);
        let cfg = tune_parallelism(&objects, THRESHOLD);
        assert_eq!(cfg.workers, 1);
        assert_eq!(cfg.concurrency_per_file, 1);
    }

    #[test]
    fn tune_single_large_file() {
        let objects = make_objects(&[500 * MB]);
        let cfg = tune_parallelism(&objects, THRESHOLD);
        assert_eq!(cfg.workers, 1);
        assert_eq!(cfg.concurrency_per_file, DEFAULT_CONCURRENCY);
    }

    #[test]
    fn tune_mixed_files_uses_median() {
        // 5 small, 4 large → median is small → small-files mode
        let mut sizes = vec![MB; 5];
        sizes.extend([500 * MB; 4]);
        let objects = make_objects(&sizes);
        let cfg = tune_parallelism(&objects, THRESHOLD);
        assert_eq!(cfg.concurrency_per_file, 1);
    }

    #[test]
    fn auto_tune_applies_config() {
        let objects = make_objects(&[MB; 50]);
        let req =
            DownloadRequest::new("bucket", "prefix/", "/tmp/dest").auto_tune(&objects, THRESHOLD);
        assert_eq!(req.workers, 50);
        assert_eq!(req.concurrency_per_file, 1);
    }
}