mediagit-storage 0.2.0

Unified async storage backend trait and implementations
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
// MediaGit - Git for Media Files
// Copyright (C) 2025 MediaGit Contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

//! AWS S3 storage backend implementation
//!
//! Provides a `StorageBackend` implementation for AWS S3 with:
//! - AWS SDK configuration using credential chains (environment, IAM, profiles)
//! - Automatic region detection
//! - Multipart upload for large files (>100MB)
//! - Concurrent part uploads for performance
//! - Exponential backoff retry logic
//! - Comprehensive error handling
//!
//! # Features
//!
//! - **Credential chain**: Automatically detects credentials from environment, IAM roles, or AWS profiles
//! - **Region detection**: Uses environment variables or AWS metadata service
//! - **Multipart uploads**: Automatically handles files >100MB with concurrent uploads
//! - **Retry logic**: Exponential backoff with configurable max retries
//! - **Performance**: Optimized for >100MB/s throughput on high-speed connections
//! - **Thread-safe**: Full `Send + Sync` support for concurrent access
//!
//! # Examples
//!
//! ```rust,no_run
//! use mediagit_storage::{StorageBackend, s3::S3Backend};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     // Create an S3 backend with default configuration
//!     let storage = S3Backend::new("my-bucket").await?;
//!
//!     // Store data
//!     storage.put("documents/resume.pdf", b"PDF content").await?;
//!
//!     // Retrieve data
//!     let data = storage.get("documents/resume.pdf").await?;
//!     assert_eq!(data, b"PDF content");
//!
//!     // Check existence
//!     if storage.exists("documents/resume.pdf").await? {
//!         println!("File exists");
//!     }
//!
//!     // List objects with prefix
//!     let documents = storage.list_objects("documents/").await?;
//!     println!("Found {} documents", documents.len());
//!
//!     // Delete object
//!     storage.delete("documents/resume.pdf").await?;
//!
//!     Ok(())
//! }
//! ```
//!
//! # Configuration
//!
//! Configuration is automatic using the AWS SDK's credential chain:
//! 1. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, etc.)
//! 2. IAM role credentials (if running on EC2, ECS, Lambda, etc.)
//! 3. AWS profiles (~/.aws/credentials and ~/.aws/config)
//!
//! Region detection:
//! 1. AWS_REGION environment variable
//! 2. AWS_DEFAULT_REGION environment variable
//! 3. From instance metadata service (if on EC2)
//!
//! # Performance
//!
//! The multipart upload implementation automatically:
//! - Uses 100MB default part size (configurable)
//! - Uploads parts concurrently (8 concurrent uploads by default)
//! - Maintains throughput >100MB/s on typical connections
//!
//! # Error Handling
//!
//! All AWS errors are mapped to `anyhow::Error` with descriptive messages.
//! Use [`StorageError`](crate::StorageError) for more structured error information.

use crate::StorageBackend;
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use aws_sdk_s3::Client;
use bytes::Bytes;
use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tracing::{debug, warn};

/// Configuration for the S3 backend
#[derive(Clone, Debug)]
pub struct S3Config {
    /// S3 bucket name
    pub bucket: String,

    /// Optional custom S3 endpoint (for S3-compatible services like MinIO)
    pub endpoint: Option<String>,

    /// Optional AWS region (for custom endpoints)
    pub region: Option<String>,

    /// Optional access key ID (for custom endpoints)
    pub access_key_id: Option<String>,

    /// Optional secret access key (for custom endpoints)
    pub secret_access_key: Option<String>,

    /// Multipart upload part size in bytes (default: 100MB)
    pub part_size: u64,

    /// Maximum number of concurrent parts to upload (default: 8)
    pub max_concurrent_parts: usize,

    /// Maximum number of retries for failed operations (default: 3)
    pub max_retries: u32,

    /// Initial retry delay in milliseconds (default: 100ms)
    pub initial_retry_delay_ms: u64,
}

impl Default for S3Config {
    fn default() -> Self {
        S3Config {
            bucket: String::new(),
            endpoint: None,
            region: None,
            access_key_id: None,
            secret_access_key: None,
            part_size: 100 * 1024 * 1024, // 100MB default
            max_concurrent_parts: 8,
            max_retries: 3,
            initial_retry_delay_ms: 100,
        }
    }
}

/// AWS S3 storage backend
///
/// Implements the `StorageBackend` trait using AWS S3.
/// Supports both standard S3 and S3-compatible services (MinIO, DigitalOcean Spaces, etc.)
///
/// # Thread Safety
///
/// This implementation is `Send + Sync` and can be safely shared across threads
/// and async tasks.
#[derive(Clone)]
pub struct S3Backend {
    client: Client,
    config: Arc<S3Config>,
    stats: Arc<S3Stats>,
}

/// Internal statistics for the S3 backend
#[derive(Debug)]
struct S3Stats {
    total_bytes_uploaded: AtomicU64,
    total_bytes_downloaded: AtomicU64,
    total_objects_deleted: AtomicU64,
}

impl S3Stats {
    fn new() -> Self {
        S3Stats {
            total_bytes_uploaded: AtomicU64::new(0),
            total_bytes_downloaded: AtomicU64::new(0),
            total_objects_deleted: AtomicU64::new(0),
        }
    }
}

impl S3Backend {
    /// Create a new S3 backend with the given bucket name
    ///
    /// Uses automatic AWS credential and region detection from:
    /// - Environment variables
    /// - IAM role (EC2, ECS, Lambda)
    /// - AWS profile files
    ///
    /// # Arguments
    ///
    /// * `bucket` - The S3 bucket name
    ///
    /// # Returns
    ///
    /// * `Ok(S3Backend)` - Successfully created backend
    /// * `Err` - If credential or region detection fails
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use mediagit_storage::s3::S3Backend;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> anyhow::Result<()> {
    /// let storage = S3Backend::new("my-bucket").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(bucket: impl Into<String>) -> Result<Self> {
        let config = S3Config {
            bucket: bucket.into(),
            ..Default::default()
        };
        Self::with_config(config).await
    }

    /// Create a new S3 backend with custom configuration
    ///
    /// # Arguments
    ///
    /// * `config` - Custom S3 configuration
    ///
    /// # Returns
    ///
    /// * `Ok(S3Backend)` - Successfully created backend
    /// * `Err` - If AWS SDK initialization fails
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use mediagit_storage::s3::{S3Backend, S3Config};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> anyhow::Result<()> {
    /// let mut config = S3Config::default();
    /// config.bucket = "my-bucket".to_string();
    /// config.endpoint = Some("https://minio.example.com".to_string());
    ///
    /// let storage = S3Backend::with_config(config).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn with_config(config: S3Config) -> Result<Self> {
        // Override endpoint if provided (for S3-compatible services like MinIO)
        // Skip aws_config::defaults().load() for custom endpoints to avoid IMDS timeouts
        let client = if let Some(endpoint) = &config.endpoint {
            debug!("Using custom S3 endpoint: {}", endpoint);
            let mut builder = aws_sdk_s3::config::Builder::new()
                .behavior_version(aws_sdk_s3::config::BehaviorVersion::latest())
                .endpoint_url(endpoint.clone())
                .force_path_style(true);
            if let Some(region) = &config.region {
                builder = builder.region(aws_sdk_s3::config::Region::new(region.clone()));
            } else {
                builder = builder.region(aws_sdk_s3::config::Region::new("us-east-1"));
            }
            if let (Some(key_id), Some(secret)) = (&config.access_key_id, &config.secret_access_key)
            {
                let credentials =
                    aws_sdk_s3::config::Credentials::new(key_id, secret, None, None, "S3Backend");
                builder = builder.credentials_provider(credentials);
            }
            Client::from_conf(builder.build())
        } else {
            // Real AWS S3 - use standard config loading (IMDS is expected)
            let sdk_config = aws_config::defaults(aws_config::BehaviorVersion::latest())
                .load()
                .await;
            Client::new(&sdk_config)
        };

        // Ensure bucket exists; use create_bucket and treat "already exists" as success.
        match client.create_bucket().bucket(&config.bucket).send().await {
            Ok(_) => {
                debug!("S3 bucket '{}' created successfully", config.bucket);
            }
            Err(e) => {
                let already_exists = e
                    .as_service_error()
                    .map(|se| se.is_bucket_already_owned_by_you() || se.is_bucket_already_exists())
                    .unwrap_or(false);
                if already_exists {
                    debug!("S3 bucket '{}' already exists, continuing", config.bucket);
                } else {
                    return Err(e).context(format!(
                        "Failed to access or create S3 bucket: {}",
                        config.bucket
                    ));
                }
            }
        }

        debug!(
            "Successfully connected to S3 bucket: {} with endpoint: {:?}",
            config.bucket,
            config.endpoint.as_deref().unwrap_or("AWS default")
        );

        Ok(S3Backend {
            client,
            config: Arc::new(config),
            stats: Arc::new(S3Stats::new()),
        })
    }

    /// Create a new S3 backend with explicit credentials
    ///
    /// Use this for S3-compatible services (B2, DigitalOcean Spaces, MinIO) that
    /// require explicit access key and secret key configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Custom S3 configuration with endpoint
    /// * `access_key` - Access Key ID
    /// * `secret_key` - Secret Access Key
    /// * `region` - Region string (e.g., "us-west-002" for B2)
    ///
    /// # Returns
    ///
    /// * `Ok(S3Backend)` - Successfully created backend
    /// * `Err` - If initialization fails
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use mediagit_storage::s3::{S3Backend, S3Config};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> anyhow::Result<()> {
    /// let config = S3Config {
    ///     bucket: "my-bucket".to_string(),
    ///     endpoint: Some("https://s3.us-west-002.backblazeb2.com".to_string()),
    ///     ..Default::default()
    /// };
    ///
    /// let storage = S3Backend::with_credentials(
    ///     config,
    ///     "access_key_id",
    ///     "secret_access_key",
    ///     "us-west-002",
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn with_credentials(
        config: S3Config,
        access_key: &str,
        secret_key: &str,
        region: &str,
    ) -> Result<Self> {
        use aws_sdk_s3::config::{Credentials, Region};

        // Create explicit credentials
        let credentials = Credentials::new(
            access_key,
            secret_key,
            None, // session token
            None, // expiry
            "mediagit-explicit-credentials",
        );

        // Build S3 config with explicit credentials and endpoint
        let mut s3_config_builder = aws_sdk_s3::config::Builder::new()
            .credentials_provider(credentials)
            .region(Region::new(region.to_string()))
            .force_path_style(true); // Required for most S3-compatible services

        if let Some(endpoint) = &config.endpoint {
            debug!("Using custom S3 endpoint with credentials: {}", endpoint);
            s3_config_builder = s3_config_builder.endpoint_url(endpoint.clone());
        }

        let client = Client::from_conf(s3_config_builder.build());

        // Ensure bucket exists; use create_bucket and ignore "already exists" errors.
        match client.create_bucket().bucket(&config.bucket).send().await {
            Ok(_) => {
                debug!(
                    "S3-compatible bucket '{}' created successfully",
                    config.bucket
                );
            }
            Err(e) => {
                let err_str = e.to_string().to_lowercase();
                if err_str.contains("bucketalreadyownedbyou")
                    || err_str.contains("bucketalreadyexists")
                    || err_str.contains("already owned")
                    || err_str.contains("already exists")
                {
                    debug!(
                        "S3-compatible bucket '{}' already exists, continuing",
                        config.bucket
                    );
                } else {
                    return Err(e).context(format!(
                        "Failed to access or create S3-compatible bucket: {}. Check credentials and endpoint.",
                        config.bucket
                    ));
                }
            }
        }

        debug!(
            "Successfully connected to S3-compatible bucket: {} at {:?}",
            config.bucket, config.endpoint
        );

        Ok(S3Backend {
            client,
            config: Arc::new(config),
            stats: Arc::new(S3Stats::new()),
        })
    }

    /// Get current statistics
    pub fn stats(&self) -> (u64, u64, u64) {
        (
            self.stats.total_bytes_uploaded.load(Ordering::Relaxed),
            self.stats.total_bytes_downloaded.load(Ordering::Relaxed),
            self.stats.total_objects_deleted.load(Ordering::Relaxed),
        )
    }

    /// Validate a key for correctness
    fn validate_key(key: &str) -> Result<()> {
        if key.is_empty() {
            return Err(anyhow!("key cannot be empty"));
        }
        if key.starts_with('/') {
            return Err(anyhow!("key cannot start with '/'"));
        }
        Ok(())
    }

    /// Perform operation with exponential backoff retry logic
    async fn with_retry<F, T>(&self, mut operation: F) -> Result<T>
    where
        F: FnMut() -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<T>> + Send>>,
    {
        let mut retry_count = 0;
        let mut delay_ms = self.config.initial_retry_delay_ms;

        loop {
            match operation().await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    retry_count += 1;
                    if retry_count >= self.config.max_retries {
                        return Err(e)
                            .context(format!("Failed after {} retries", self.config.max_retries));
                    }

                    warn!(
                        "Operation failed (attempt {}/{}), retrying in {}ms: {}",
                        retry_count, self.config.max_retries, delay_ms, e
                    );

                    tokio::time::sleep(tokio::time::Duration::from_millis(delay_ms)).await;

                    // Exponential backoff with jitter
                    delay_ms = (delay_ms * 2).min(10000); // Cap at 10 seconds
                }
            }
        }
    }
}

impl fmt::Debug for S3Backend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("S3Backend")
            .field("bucket", &self.config.bucket)
            .field("endpoint", &self.config.endpoint)
            .field("part_size", &self.config.part_size)
            .field("max_concurrent_parts", &self.config.max_concurrent_parts)
            .finish()
    }
}

#[async_trait]
impl StorageBackend for S3Backend {
    /// Retrieve an object from S3
    ///
    /// # Arguments
    ///
    /// * `key` - The object key (must be non-empty and not start with '/')
    ///
    /// # Returns
    ///
    /// * `Ok(Vec<u8>)` - The object data
    /// * `Err` - If the key doesn't exist or an error occurs
    async fn get(&self, key: &str) -> Result<Vec<u8>> {
        Self::validate_key(key)?;

        let client = self.client.clone();
        let bucket = self.config.bucket.clone();
        let key_clone = key.to_string();
        let stats = self.stats.clone();

        self.with_retry(|| {
            let client = client.clone();
            let bucket = bucket.clone();
            let key = key_clone.clone();
            let stats = stats.clone();

            Box::pin(async move {
                debug!("Getting object from S3: {}", key);

                let response = client
                    .get_object()
                    .bucket(&bucket)
                    .key(&key)
                    .send()
                    .await
                    .map_err(|e| anyhow!("Failed to get object: {}", e))?;

                let body = response
                    .body
                    .collect()
                    .await
                    .map_err(|e| anyhow!("Failed to read object body: {}", e))?;

                let data = body.into_bytes().to_vec();
                stats
                    .total_bytes_downloaded
                    .fetch_add(data.len() as u64, Ordering::Relaxed);

                Ok(data)
            })
        })
        .await
    }

    /// Store an object in S3
    ///
    /// For objects smaller than the configured part size, uses direct put_object.
    /// For larger objects, automatically uses multipart upload.
    ///
    /// # Arguments
    ///
    /// * `key` - The object key (must be non-empty and not start with '/')
    /// * `data` - The object content
    ///
    /// # Returns
    ///
    /// * `Ok(())` - The operation succeeded
    /// * `Err` - If an error occurs
    async fn put(&self, key: &str, data: &[u8]) -> Result<()> {
        Self::validate_key(key)?;

        // For small objects, use simple put_object
        if data.len() as u64 <= self.config.part_size {
            return self.put_simple(key, data).await;
        }

        // For large objects, use multipart upload
        self.put_multipart(key, data).await
    }

    /// Check if an object exists in S3
    ///
    /// # Arguments
    ///
    /// * `key` - The object key (must be non-empty and not start with '/')
    ///
    /// # Returns
    ///
    /// * `Ok(true)` - The object exists
    /// * `Ok(false)` - The object doesn't exist
    /// * `Err` - If an error occurs
    async fn exists(&self, key: &str) -> Result<bool> {
        Self::validate_key(key)?;

        let client = self.client.clone();
        let bucket = self.config.bucket.clone();
        let key_clone = key.to_string();

        self.with_retry(|| {
            let client = client.clone();
            let bucket = bucket.clone();
            let key = key_clone.clone();

            Box::pin(async move {
                debug!("Checking if object exists in S3: {}", key);

                match client.head_object().bucket(&bucket).key(&key).send().await {
                    Ok(_) => {
                        debug!("Object exists: {}", key);
                        Ok(true)
                    }
                    Err(e) => {
                        let error_message = e.to_string().to_lowercase();
                        // Check for various "not found" patterns from real and emulated S3 services
                        if error_message.contains("404")
                            || error_message.contains("not found")
                            || error_message.contains("notfound")
                            || error_message.contains("nosuchkey")
                            || error_message.contains("does not exist")
                            || error_message.contains("no such key")
                            // LocalStack emulator sometimes returns generic "service error" for non-existent objects
                            || (error_message.contains("service error") && error_message.len() < 50)
                        {
                            debug!("Object does not exist: {}", key);
                            Ok(false)
                        } else {
                            Err(anyhow!("Failed to check object existence: {}", e))
                        }
                    }
                }
            })
        })
        .await
    }

    /// Delete an object from S3
    ///
    /// This operation is idempotent: deleting a non-existent object succeeds.
    ///
    /// # Arguments
    ///
    /// * `key` - The object key (must be non-empty and not start with '/')
    ///
    /// # Returns
    ///
    /// * `Ok(())` - The operation succeeded
    /// * `Err` - If an error occurs
    async fn delete(&self, key: &str) -> Result<()> {
        Self::validate_key(key)?;

        let client = self.client.clone();
        let bucket = self.config.bucket.clone();
        let key_clone = key.to_string();
        let stats = self.stats.clone();

        self.with_retry(|| {
            let client = client.clone();
            let bucket = bucket.clone();
            let key = key_clone.clone();
            let stats = stats.clone();

            Box::pin(async move {
                debug!("Deleting object from S3: {}", key);

                client
                    .delete_object()
                    .bucket(&bucket)
                    .key(&key)
                    .send()
                    .await
                    .map_err(|e| anyhow!("Failed to delete object: {}", e))?;

                stats.total_objects_deleted.fetch_add(1, Ordering::Relaxed);

                Ok(())
            })
        })
        .await
    }

    /// List objects in S3 with a given prefix
    ///
    /// Returns a sorted list of all keys that start with the given prefix.
    ///
    /// # Arguments
    ///
    /// * `prefix` - The key prefix to filter by (can be empty to list all)
    ///
    /// # Returns
    ///
    /// * `Ok(Vec<String>)` - Sorted list of matching keys
    /// * `Err` - If an error occurs
    async fn list_objects(&self, prefix: &str) -> Result<Vec<String>> {
        let client = self.client.clone();
        let bucket = self.config.bucket.clone();
        let prefix_clone = prefix.to_string();

        self.with_retry(|| {
            let client = client.clone();
            let bucket = bucket.clone();
            let prefix = prefix_clone.clone();

            Box::pin(async move {
                debug!("Listing objects in S3 with prefix: '{}'", prefix);

                let mut result = vec![];
                let mut continuation_token: Option<String> = None;

                loop {
                    let mut request = client.list_objects_v2().bucket(&bucket);

                    if !prefix.is_empty() {
                        request = request.prefix(&prefix);
                    }

                    if let Some(token) = continuation_token {
                        request = request.continuation_token(token);
                    }

                    let response = request
                        .send()
                        .await
                        .map_err(|e| anyhow!("Failed to list objects: {}", e))?;

                    // Collect keys from this page
                    for obj in response.contents() {
                        if let Some(key) = obj.key() {
                            result.push(key.to_string());
                        }
                    }

                    // Check if there are more results
                    if response.is_truncated() == Some(true) {
                        continuation_token =
                            response.next_continuation_token().map(|t| t.to_string());
                    } else {
                        break;
                    }
                }

                // Sort for consistency
                result.sort();

                debug!("Found {} objects with prefix: '{}'", result.len(), prefix);
                Ok(result)
            })
        })
        .await
    }
}

// Helper methods for S3Backend (not part of StorageBackend trait)
impl S3Backend {
    /// Upload small objects using direct put_object
    async fn put_simple(&self, key: &str, data: &[u8]) -> Result<()> {
        debug!("Putting small object to S3: {} ({} bytes)", key, data.len());

        let client = self.client.clone();
        let bucket = self.config.bucket.clone();
        let key_clone = key.to_string();
        let data_vec = data.to_vec();
        let stats = self.stats.clone();

        self.with_retry(|| {
            let client = client.clone();
            let bucket = bucket.clone();
            let key = key_clone.clone();
            let data = data_vec.clone();
            let stats = stats.clone();

            Box::pin(async move {
                client
                    .put_object()
                    .bucket(&bucket)
                    .key(&key)
                    .body(Bytes::from(data.clone()).into())
                    .send()
                    .await
                    .map_err(|e| anyhow!("Failed to put object: {}", e))?;

                stats
                    .total_bytes_uploaded
                    .fetch_add(data.len() as u64, Ordering::Relaxed);

                debug!("Successfully put object to S3: {}", key);
                Ok(())
            })
        })
        .await
    }

    /// Upload large objects using multipart upload
    async fn put_multipart(&self, key: &str, data: &[u8]) -> Result<()> {
        debug!(
            "Putting large object to S3 (multipart): {} ({} bytes)",
            key,
            data.len()
        );

        let client = self.client.clone();
        let bucket = self.config.bucket.clone();
        let key_clone = key.to_string();

        // Initiate multipart upload
        let multipart = client
            .create_multipart_upload()
            .bucket(&bucket)
            .key(&key_clone)
            .send()
            .await
            .map_err(|e| anyhow!("Failed to initiate multipart upload: {}", e))?;

        let upload_id = multipart
            .upload_id()
            .ok_or_else(|| anyhow!("No upload ID returned from S3"))?
            .to_string();

        debug!(
            "Initiated multipart upload for {}: {}",
            key_clone, upload_id
        );

        // Upload parts concurrently
        let mut part_handles = vec![];
        let part_size = self.config.part_size as usize;
        let mut part_number = 1;

        for chunk in data.chunks(part_size) {
            let client = client.clone();
            let bucket = bucket.clone();
            let key = key_clone.clone();
            let upload_id = upload_id.clone();
            let stats = self.stats.clone();
            let chunk_data = chunk.to_vec();
            let part_num = part_number;

            let handle = tokio::spawn(async move {
                debug!(
                    "Uploading part {} ({} bytes) for key: {}",
                    part_num,
                    chunk_data.len(),
                    key
                );

                let response = client
                    .upload_part()
                    .bucket(&bucket)
                    .key(&key)
                    .upload_id(&upload_id)
                    .part_number(part_num)
                    .body(Bytes::from(chunk_data.clone()).into())
                    .send()
                    .await
                    .map_err(|e| anyhow!("Failed to upload part {}: {}", part_num, e))?;

                let etag = response
                    .e_tag()
                    .ok_or_else(|| anyhow!("No ETag returned for part {}", part_num))?
                    .to_string();

                stats
                    .total_bytes_uploaded
                    .fetch_add(chunk_data.len() as u64, Ordering::Relaxed);

                Ok::<_, anyhow::Error>((part_num, etag))
            });

            part_handles.push(handle);

            // Limit concurrent uploads
            if part_handles.len() >= self.config.max_concurrent_parts {
                // Wait for one to complete before starting more
                if let Some(handle) = part_handles.pop() {
                    let _ = handle.await??;
                }
            }

            part_number += 1;
        }

        // Wait for all remaining parts to complete
        let mut parts = vec![];
        for handle in part_handles {
            let (part_num, etag) = handle.await??;
            parts.push((part_num, etag));
        }

        // Sort parts by part number
        parts.sort_by_key(|p| p.0);

        // Complete multipart upload
        let part_list: Vec<_> = parts
            .into_iter()
            .map(|(part_num, etag)| {
                aws_sdk_s3::types::CompletedPart::builder()
                    .part_number(part_num)
                    .e_tag(etag)
                    .build()
            })
            .collect();

        client
            .complete_multipart_upload()
            .bucket(&bucket)
            .key(&key_clone)
            .upload_id(&upload_id)
            .multipart_upload(
                aws_sdk_s3::types::CompletedMultipartUpload::builder()
                    .set_parts(Some(part_list))
                    .build(),
            )
            .send()
            .await
            .map_err(|e| anyhow!("Failed to complete multipart upload: {}", e))?;

        debug!("Successfully completed multipart upload for {}", key_clone);
        Ok(())
    }
}

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

    #[test]
    fn test_default_config() {
        let config = S3Config::default();
        assert_eq!(config.part_size, 100 * 1024 * 1024);
        assert_eq!(config.max_concurrent_parts, 8);
        assert_eq!(config.max_retries, 3);
        assert_eq!(config.initial_retry_delay_ms, 100);
    }

    #[test]
    fn test_validate_key() {
        assert!(S3Backend::validate_key("valid_key").is_ok());
        assert!(S3Backend::validate_key("path/to/key").is_ok());
        assert!(S3Backend::validate_key("").is_err());
        assert!(S3Backend::validate_key("/invalid").is_err());
    }

    #[test]
    fn test_debug_impl() {
        let config = S3Config {
            bucket: "test-bucket".to_string(),
            ..Default::default()
        };
        let _ = format!("{:?}", config);
    }
}