s-zip 0.11.3

High-performance streaming ZIP library with AES-256 encryption and async/await support - Read/write ZIP files with minimal memory footprint. Supports password protection, cloud storage, and Tokio runtime.
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
//! AWS S3 streaming adapter using multipart upload.
//!
//! This module provides `S3ZipWriter` which implements `AsyncWrite + AsyncSeek + Unpin`,
//! enabling `AsyncStreamingZipWriter` to stream ZIP files directly to S3 without loading
//! the entire archive into memory.
//!
//! ## How it Works
//!
//! - Uses S3 multipart upload (minimum 5MB per part, except the last part)
//! - Buffers writes until reaching part size threshold
//! - Uploads parts in the background using Tokio tasks
//! - Tracks virtual position for ZIP central directory (no actual seeking)
//! - Maintains constant memory usage (~5-10MB)
//!
//! ## Example
//!
//! ```no_run
//! use s_zip::{AsyncStreamingZipWriter, cloud::S3ZipWriter};
//! use aws_sdk_s3::Client;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = aws_config::load_from_env().await;
//! let s3_client = Client::new(&config);
//!
//! let writer = S3ZipWriter::new(s3_client, "my-bucket", "exports/data.zip").await?;
//! let mut zip = AsyncStreamingZipWriter::from_writer(writer);
//!
//! zip.start_entry("file.txt").await?;
//! zip.write_data(b"Hello S3!").await?;
//! zip.finish().await?;
//! # Ok(())
//! # }
//! ```

use crate::error::{Result, SZipError};
use aws_sdk_s3::primitives::ByteStream;
use aws_sdk_s3::types::{CompletedMultipartUpload, CompletedPart};
use aws_sdk_s3::Client;
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::io::{AsyncSeek, AsyncWrite};
use tokio::sync::mpsc;

/// Default part size for S3 multipart upload (5MB - S3 minimum)
pub const DEFAULT_PART_SIZE: usize = 5 * 1024 * 1024;

/// Maximum part size (5GB - S3 maximum)
pub const MAX_PART_SIZE: usize = 5 * 1024 * 1024 * 1024;

/// Maximum number of parts (S3 limit)
pub const MAX_PARTS: usize = 10_000;

/// S3 ZIP writer that streams directly to S3 using multipart upload.
///
/// This writer implements `AsyncWrite + AsyncSeek + Unpin`, making it compatible
/// with `AsyncStreamingZipWriter`.
pub struct S3ZipWriter {
    /// Upload state (managed by background task)
    upload_tx: mpsc::UnboundedSender<UploadCommand>,
    upload_task: Option<tokio::task::JoinHandle<Result<()>>>,

    /// Write buffer (accumulates data until part_size)
    buffer: Vec<u8>,
    part_size: usize,

    /// Virtual position tracking (for ZIP central directory)
    position: u64,

    /// Current part number
    current_part_number: usize,

    /// Flag to prevent sending Complete command multiple times
    shutdown_initiated: bool,

    /// Maximum concurrent part uploads (default: 4)
    #[allow(dead_code)]
    max_concurrent_uploads: usize,
}

/// Commands sent to the background upload task
enum UploadCommand {
    /// Upload a part with given data
    UploadPart { part_number: usize, data: Vec<u8> },
    /// Complete the upload with optional final part
    Complete { final_data: Option<Vec<u8>> },
}

/// Builder for `S3ZipWriter` with configuration options.
///
/// Supports MinIO and other S3-compatible storage services by allowing
/// custom endpoint URLs.
///
/// ## Using with MinIO
///
/// ```no_run
/// # use s_zip::cloud::S3ZipWriter;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let writer = S3ZipWriter::builder()
///     .endpoint_url("http://localhost:9000")
///     .region("us-east-1")
///     .bucket("my-bucket")
///     .key("archive.zip")
///     .build()
///     .await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Using with Cloudflare R2
///
/// ```no_run
/// # use s_zip::cloud::S3ZipWriter;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let writer = S3ZipWriter::builder()
///     .endpoint_url("https://<account_id>.r2.cloudflarestorage.com")
///     .bucket("my-bucket")
///     .key("archive.zip")
///     .build()
///     .await?;
/// # Ok(())
/// # }
/// ```
pub struct S3ZipWriterBuilder {
    client: Option<Client>,
    bucket: String,
    key: String,
    part_size: usize,
    endpoint_url: Option<String>,
    region: Option<String>,
    force_path_style: bool,
    max_concurrent_uploads: usize,
}

impl S3ZipWriter {
    /// Create a new S3 ZIP writer with default settings.
    ///
    /// Uses 5MB part size (S3 minimum).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use s_zip::cloud::S3ZipWriter;
    /// # use aws_sdk_s3::Client;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = aws_config::load_from_env().await;
    /// let client = Client::new(&config);
    ///
    /// let writer = S3ZipWriter::new(
    ///     client,
    ///     "my-bucket",
    ///     "exports/archive.zip"
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(
        client: Client,
        bucket: impl Into<String>,
        key: impl Into<String>,
    ) -> Result<Self> {
        Self::builder()
            .client(client)
            .bucket(bucket)
            .key(key)
            .build()
            .await
    }

    /// Create a builder for configuring the S3 writer.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use s_zip::cloud::S3ZipWriter;
    /// # use aws_sdk_s3::Client;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = Client::new(&aws_config::load_from_env().await);
    ///
    /// let writer = S3ZipWriter::builder()
    ///     .client(client)
    ///     .bucket("my-bucket")
    ///     .key("large-archive.zip")
    ///     .part_size(100 * 1024 * 1024)  // 100MB parts for huge files
    ///     .max_concurrent_uploads(8)      // Upload 8 parts in parallel
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder() -> S3ZipWriterBuilder {
        S3ZipWriterBuilder {
            client: None,
            bucket: String::new(),
            key: String::new(),
            part_size: DEFAULT_PART_SIZE,
            endpoint_url: None,
            region: None,
            force_path_style: false,
            max_concurrent_uploads: 4, // Default: 4 concurrent uploads
        }
    }
}

impl S3ZipWriterBuilder {
    /// Set a pre-configured S3 client.
    ///
    /// If not set, a client will be created automatically using environment
    /// credentials and any configured endpoint/region.
    pub fn client(mut self, client: Client) -> Self {
        self.client = Some(client);
        self
    }

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

    /// Set the S3 object key (path).
    pub fn key(mut self, key: impl Into<String>) -> Self {
        self.key = key.into();
        self
    }

    /// Set a custom endpoint URL for S3-compatible services.
    ///
    /// Use this for MinIO, Cloudflare R2, DigitalOcean Spaces, Backblaze B2,
    /// Linode Object Storage, and other S3-compatible services.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use s_zip::cloud::S3ZipWriter;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // MinIO
    /// let writer = S3ZipWriter::builder()
    ///     .endpoint_url("http://localhost:9000")
    ///     .bucket("my-bucket")
    ///     .key("archive.zip")
    ///     .build()
    ///     .await?;
    ///
    /// // Cloudflare R2
    /// let writer = S3ZipWriter::builder()
    ///     .endpoint_url("https://account_id.r2.cloudflarestorage.com")
    ///     .bucket("my-bucket")
    ///     .key("archive.zip")
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn endpoint_url(mut self, url: impl Into<String>) -> Self {
        self.endpoint_url = Some(url.into());
        self
    }

    /// Set the AWS region.
    ///
    /// For MinIO and some S3-compatible services, you may need to set this
    /// to a specific value (e.g., "us-east-1").
    pub fn region(mut self, region: impl Into<String>) -> Self {
        self.region = Some(region.into());
        self
    }

    /// Force path-style URLs instead of virtual-hosted style.
    ///
    /// Required for MinIO and some S3-compatible services that don't support
    /// virtual-hosted style URLs (e.g., `http://endpoint/bucket/key` instead of
    /// `http://bucket.endpoint/key`).
    ///
    /// Default: `false` (uses virtual-hosted style for AWS S3)
    pub fn force_path_style(mut self, force: bool) -> Self {
        self.force_path_style = force;
        self
    }

    /// Set the part size for multipart upload.
    ///
    /// Must be at least 5MB (except the final part). Larger parts reduce the number
    /// of API calls but increase memory usage.
    ///
    /// # Panics
    ///
    /// Panics if part_size < 5MB or > 5GB.
    pub fn part_size(mut self, part_size: usize) -> Self {
        assert!(
            part_size >= DEFAULT_PART_SIZE,
            "Part size must be at least 5MB"
        );
        assert!(part_size <= MAX_PART_SIZE, "Part size must not exceed 5GB");
        self.part_size = part_size;
        self
    }

    /// Set maximum number of concurrent part uploads.
    ///
    /// Higher values increase throughput but use more network connections.
    /// Default is 4, which provides good balance between speed and resource usage.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use s_zip::cloud::S3ZipWriter;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let writer = S3ZipWriter::builder()
    ///     .bucket("my-bucket")
    ///     .key("archive.zip")
    ///     .max_concurrent_uploads(8)  // 8 parts in parallel for faster uploads
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn max_concurrent_uploads(mut self, max: usize) -> Self {
        assert!(max > 0, "max_concurrent_uploads must be at least 1");
        assert!(max <= 20, "max_concurrent_uploads should not exceed 20");
        self.max_concurrent_uploads = max;
        self
    }

    /// Build the S3 writer and start the background upload task.
    ///
    /// If no client was provided, one will be created using environment credentials
    /// and any configured endpoint/region settings.
    pub async fn build(self) -> Result<S3ZipWriter> {
        let client = match self.client {
            Some(c) => c,
            None => {
                // Build client from configuration
                let mut config_loader = aws_config::from_env();

                if let Some(ref endpoint) = self.endpoint_url {
                    config_loader = config_loader.endpoint_url(endpoint);
                }

                if let Some(ref region) = self.region {
                    config_loader = config_loader.region(aws_config::Region::new(region.clone()));
                }

                let sdk_config = config_loader.load().await;

                let mut s3_config = aws_sdk_s3::config::Builder::from(&sdk_config);

                if self.force_path_style {
                    s3_config = s3_config.force_path_style(true);
                }

                Client::from_conf(s3_config.build())
            }
        };

        let (tx, rx) = mpsc::unbounded_channel();

        // Spawn background task for uploading parts with concurrent support
        let max_concurrent = self.max_concurrent_uploads;
        let upload_task = tokio::spawn(upload_worker_concurrent(
            client,
            self.bucket,
            self.key,
            rx,
            max_concurrent,
        ));

        Ok(S3ZipWriter {
            upload_tx: tx,
            upload_task: Some(upload_task),
            buffer: Vec::with_capacity(self.part_size),
            part_size: self.part_size,
            position: 0,
            current_part_number: 0,
            shutdown_initiated: false,
            max_concurrent_uploads: self.max_concurrent_uploads,
        })
    }
}

impl AsyncWrite for S3ZipWriter {
    fn poll_write(
        mut self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        // Add data to buffer
        self.buffer.extend_from_slice(buf);
        self.position += buf.len() as u64;

        // Check if we should flush a part
        if self.buffer.len() >= self.part_size {
            let part_size = self.part_size;
            let data = std::mem::replace(&mut self.buffer, Vec::with_capacity(part_size));
            self.current_part_number += 1;

            // Send to background task (non-blocking)
            if self
                .upload_tx
                .send(UploadCommand::UploadPart {
                    part_number: self.current_part_number,
                    data,
                })
                .is_err()
            {
                return Poll::Ready(Err(io::Error::new(
                    io::ErrorKind::BrokenPipe,
                    "Upload task terminated unexpectedly",
                )));
            }
        }

        Poll::Ready(Ok(buf.len()))
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        // Flushing is handled by the background task
        Poll::Ready(Ok(()))
    }

    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        // Only send Complete command once
        if !self.shutdown_initiated {
            self.shutdown_initiated = true;

            // Send final part (if any) and complete upload
            let final_data = if !self.buffer.is_empty() {
                Some(std::mem::take(&mut self.buffer))
            } else {
                None
            };

            // Send completion command
            if self
                .upload_tx
                .send(UploadCommand::Complete { final_data })
                .is_err()
            {
                return Poll::Ready(Err(io::Error::new(
                    io::ErrorKind::BrokenPipe,
                    "Upload task terminated unexpectedly",
                )));
            }
        }

        // Wait for background task to complete
        if let Some(task) = self.upload_task.as_mut() {
            match Pin::new(task).poll(cx) {
                Poll::Ready(Ok(Ok(()))) => Poll::Ready(Ok(())),
                Poll::Ready(Ok(Err(e))) => {
                    Poll::Ready(Err(io::Error::other(format!("S3 upload failed: {}", e))))
                }
                Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(format!(
                    "Upload task panicked: {}",
                    e
                )))),
                Poll::Pending => Poll::Pending,
            }
        } else {
            Poll::Ready(Ok(()))
        }
    }
}

impl AsyncSeek for S3ZipWriter {
    fn start_seek(self: Pin<&mut Self>, position: io::SeekFrom) -> io::Result<()> {
        // S3 doesn't support seeking - we only track virtual position
        match position {
            io::SeekFrom::Current(0) => Ok(()), // Query current position (allowed)
            _ => Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "S3 writer does not support seeking",
            )),
        }
    }

    fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
        // Return tracked virtual position
        Poll::Ready(Ok(self.position))
    }
}

impl Unpin for S3ZipWriter {}

/// Background worker that handles S3 multipart upload operations (sequential, deprecated).
///
/// This is the old sequential implementation kept for reference.
/// Use `upload_worker_concurrent` for better performance.
#[allow(dead_code)]
async fn upload_worker(
    client: Client,
    bucket: String,
    key: String,
    mut rx: mpsc::UnboundedReceiver<UploadCommand>,
) -> Result<()> {
    let mut upload_id: Option<String> = None;
    let mut parts: Vec<CompletedPart> = Vec::new();

    while let Some(cmd) = rx.recv().await {
        match cmd {
            UploadCommand::UploadPart { part_number, data } => {
                // Initialize multipart upload if first part
                if upload_id.is_none() {
                    let response = client
                        .create_multipart_upload()
                        .bucket(&bucket)
                        .key(&key)
                        .send()
                        .await
                        .map_err(|e| {
                            SZipError::Io(io::Error::other(format!(
                                "Failed to create multipart upload: {}",
                                e
                            )))
                        })?;

                    upload_id = Some(
                        response
                            .upload_id()
                            .ok_or_else(|| {
                                SZipError::Io(io::Error::other("No upload_id returned from S3"))
                            })?
                            .to_string(),
                    );
                }

                // Upload part
                let response = client
                    .upload_part()
                    .bucket(&bucket)
                    .key(&key)
                    .upload_id(upload_id.as_ref().unwrap())
                    .part_number(part_number as i32)
                    .body(ByteStream::from(data))
                    .send()
                    .await
                    .map_err(|e| {
                        SZipError::Io(io::Error::other(format!(
                            "Failed to upload part {}: {}",
                            part_number, e
                        )))
                    })?;

                let etag = response
                    .e_tag()
                    .ok_or_else(|| {
                        SZipError::Io(io::Error::other(format!(
                            "No ETag returned for part {}",
                            part_number
                        )))
                    })?
                    .to_string();

                parts.push(
                    CompletedPart::builder()
                        .part_number(part_number as i32)
                        .e_tag(etag)
                        .build(),
                );
            }
            UploadCommand::Complete { final_data } => {
                // Upload final part if any data remains
                if let Some(data) = final_data {
                    if !data.is_empty() {
                        // Initialize upload if this is the only part
                        if upload_id.is_none() {
                            let response = client
                                .create_multipart_upload()
                                .bucket(&bucket)
                                .key(&key)
                                .send()
                                .await
                                .map_err(|e| {
                                    SZipError::Io(io::Error::other(format!(
                                        "Failed to create multipart upload: {}",
                                        e
                                    )))
                                })?;

                            upload_id = Some(
                                response
                                    .upload_id()
                                    .ok_or_else(|| {
                                        SZipError::Io(io::Error::other(
                                            "No upload_id returned from S3",
                                        ))
                                    })?
                                    .to_string(),
                            );
                        }

                        let part_number = parts.len() + 1;
                        let response = client
                            .upload_part()
                            .bucket(&bucket)
                            .key(&key)
                            .upload_id(upload_id.as_ref().unwrap())
                            .part_number(part_number as i32)
                            .body(ByteStream::from(data))
                            .send()
                            .await
                            .map_err(|e| {
                                SZipError::Io(io::Error::other(format!(
                                    "Failed to upload final part: {}",
                                    e
                                )))
                            })?;

                        let etag = response
                            .e_tag()
                            .ok_or_else(|| {
                                SZipError::Io(io::Error::other("No ETag returned for final part"))
                            })?
                            .to_string();

                        parts.push(
                            CompletedPart::builder()
                                .part_number(part_number as i32)
                                .e_tag(etag)
                                .build(),
                        );
                    }
                }

                // Complete multipart upload
                if let Some(id) = upload_id {
                    client
                        .complete_multipart_upload()
                        .bucket(&bucket)
                        .key(&key)
                        .upload_id(&id)
                        .multipart_upload(
                            CompletedMultipartUpload::builder()
                                .set_parts(Some(parts))
                                .build(),
                        )
                        .send()
                        .await
                        .map_err(|e| {
                            SZipError::Io(io::Error::other(format!(
                                "Failed to complete multipart upload: {}",
                                e
                            )))
                        })?;
                }

                break;
            }
        }
    }

    Ok(())
}

/// Concurrent upload worker with retry logic and parallel uploads
///
/// This version uploads multiple parts in parallel for 3-5x faster S3 uploads.
/// Includes automatic retry with exponential backoff for transient failures.
async fn upload_worker_concurrent(
    client: Client,
    bucket: String,
    key: String,
    mut rx: mpsc::UnboundedReceiver<UploadCommand>,
    max_concurrent: usize,
) -> Result<()> {
    use futures_util::stream::{FuturesUnordered, StreamExt};

    let client = Arc::new(client);
    let bucket = Arc::new(bucket);
    let key = Arc::new(key);
    let mut upload_id: Option<String> = None;
    let mut completed_parts: Vec<(usize, CompletedPart)> = Vec::new();
    let mut upload_futures = FuturesUnordered::new();
    let mut pending_parts: Vec<(usize, Vec<u8>)> = Vec::new();

    while let Some(cmd) = rx.recv().await {
        match cmd {
            UploadCommand::UploadPart { part_number, data } => {
                // Initialize multipart upload if first part
                if upload_id.is_none() {
                    let response = client
                        .create_multipart_upload()
                        .bucket(bucket.as_ref())
                        .key(key.as_ref())
                        .send()
                        .await
                        .map_err(|e| {
                            SZipError::Io(io::Error::other(format!(
                                "Failed to create multipart upload: {}",
                                e
                            )))
                        })?;

                    upload_id = Some(
                        response
                            .upload_id()
                            .ok_or_else(|| {
                                SZipError::Io(io::Error::other("No upload_id returned from S3"))
                            })?
                            .to_string(),
                    );
                }

                let upload_id_clone = upload_id.clone().unwrap();

                // Add to pending or start upload immediately
                if upload_futures.len() < max_concurrent {
                    // Start upload immediately
                    let fut = upload_part_with_retry(
                        client.clone(),
                        bucket.clone(),
                        key.clone(),
                        upload_id_clone,
                        part_number,
                        data,
                    );
                    upload_futures.push(fut);
                } else {
                    // Queue for later
                    pending_parts.push((part_number, data));
                }

                // Poll for completed uploads
                while let Some(result) = upload_futures.next().await {
                    let (part_num, completed_part) = result?;
                    completed_parts.push((part_num, completed_part));

                    // Start next pending upload if any
                    if let Some((pn, pdata)) = pending_parts.pop() {
                        let fut = upload_part_with_retry(
                            client.clone(),
                            bucket.clone(),
                            key.clone(),
                            upload_id.clone().unwrap(),
                            pn,
                            pdata,
                        );
                        upload_futures.push(fut);
                    }

                    // Break if we haven't reached max concurrent yet
                    if upload_futures.len() < max_concurrent {
                        break;
                    }
                }
            }
            UploadCommand::Complete { final_data } => {
                // Upload final part if any data remains
                if let Some(data) = final_data {
                    if !data.is_empty() {
                        // Initialize upload if this is the only part
                        if upload_id.is_none() {
                            let response = client
                                .create_multipart_upload()
                                .bucket(bucket.as_ref())
                                .key(key.as_ref())
                                .send()
                                .await
                                .map_err(|e| {
                                    SZipError::Io(io::Error::other(format!(
                                        "Failed to create multipart upload: {}",
                                        e
                                    )))
                                })?;

                            upload_id = Some(
                                response
                                    .upload_id()
                                    .ok_or_else(|| {
                                        SZipError::Io(io::Error::other(
                                            "No upload_id returned from S3",
                                        ))
                                    })?
                                    .to_string(),
                            );
                        }

                        let part_number =
                            completed_parts.len() + upload_futures.len() + pending_parts.len() + 1;
                        let fut = upload_part_with_retry(
                            client.clone(),
                            bucket.clone(),
                            key.clone(),
                            upload_id.clone().unwrap(),
                            part_number,
                            data,
                        );
                        upload_futures.push(fut);
                    }
                }

                // Wait for all remaining uploads to complete
                while let Some(result) = upload_futures.next().await {
                    let (part_num, completed_part) = result?;
                    completed_parts.push((part_num, completed_part));
                }

                // Sort parts by part number (S3 requires sequential order)
                completed_parts.sort_by_key(|(part_num, _)| *part_num);
                let parts: Vec<_> = completed_parts.into_iter().map(|(_, p)| p).collect();

                // Complete multipart upload
                if let Some(id) = upload_id {
                    client
                        .complete_multipart_upload()
                        .bucket(bucket.as_ref())
                        .key(key.as_ref())
                        .upload_id(&id)
                        .multipart_upload(
                            CompletedMultipartUpload::builder()
                                .set_parts(Some(parts))
                                .build(),
                        )
                        .send()
                        .await
                        .map_err(|e| {
                            SZipError::Io(io::Error::other(format!(
                                "Failed to complete multipart upload: {}",
                                e
                            )))
                        })?;
                }

                break;
            }
        }
    }

    Ok(())
}

/// Upload a single part with exponential backoff retry
async fn upload_part_with_retry(
    client: Arc<Client>,
    bucket: Arc<String>,
    key: Arc<String>,
    upload_id: String,
    part_number: usize,
    data: Vec<u8>,
) -> Result<(usize, CompletedPart)> {
    const MAX_RETRIES: u32 = 3;
    const BASE_DELAY_MS: u64 = 100;

    let mut retries = 0;

    loop {
        match client
            .upload_part()
            .bucket(bucket.as_ref())
            .key(key.as_ref())
            .upload_id(&upload_id)
            .part_number(part_number as i32)
            .body(ByteStream::from(data.clone()))
            .send()
            .await
        {
            Ok(response) => {
                let etag = response
                    .e_tag()
                    .ok_or_else(|| {
                        SZipError::Io(io::Error::other(format!(
                            "No ETag returned for part {}",
                            part_number
                        )))
                    })?
                    .to_string();

                let completed_part = CompletedPart::builder()
                    .part_number(part_number as i32)
                    .e_tag(etag)
                    .build();

                return Ok((part_number, completed_part));
            }
            Err(_e) if retries < MAX_RETRIES => {
                retries += 1;
                let delay = BASE_DELAY_MS * 2_u64.pow(retries - 1);
                tokio::time::sleep(Duration::from_millis(delay)).await;
                // Continue loop to retry
            }
            Err(e) => {
                return Err(SZipError::Io(io::Error::other(format!(
                    "Failed to upload part {} after {} retries: {}",
                    part_number, MAX_RETRIES, e
                ))));
            }
        }
    }
}

impl Drop for S3ZipWriter {
    fn drop(&mut self) {
        // If the writer is dropped without calling finish(), we should try to abort
        // the multipart upload to avoid orphaned parts
        // However, we can't easily abort from Drop since it's not async
        // Users should ensure finish() is called properly
    }
}

// ============================================================================
// S3 ZIP Reader
// ============================================================================

use tokio::io::AsyncRead;

/// S3 ZIP reader that reads ZIP files directly from S3.
///
/// This reader implements `AsyncRead + AsyncSeek + Unpin + Send`, making it compatible
/// with `GenericAsyncZipReader`.
///
/// ## Example
///
/// ```no_run
/// use s_zip::{GenericAsyncZipReader, cloud::S3ZipReader};
/// use aws_sdk_s3::Client;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let config = aws_config::load_from_env().await;
/// let s3_client = Client::new(&config);
///
/// let reader = S3ZipReader::new(s3_client, "my-bucket", "archive.zip").await?;
/// let mut zip = GenericAsyncZipReader::new(reader).await?;
///
/// // List entries
/// for entry in zip.entries() {
///     println!("{}: {} bytes", entry.name, entry.uncompressed_size);
/// }
///
/// // Read a file
/// let data = zip.read_entry_by_name("file.txt").await?;
/// # Ok(())
/// # }
/// ```
pub struct S3ZipReader {
    client: Client,
    bucket: String,
    key: String,
    position: u64,
    size: u64,
    #[allow(clippy::type_complexity)]
    read_future: Option<Pin<Box<dyn Future<Output = io::Result<Vec<u8>>> + Send>>>,
}

/// Builder for `S3ZipReader` with configuration options.
///
/// Supports MinIO and other S3-compatible storage services.
///
/// ## Using with MinIO
///
/// ```no_run
/// # use s_zip::cloud::S3ZipReader;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let reader = S3ZipReader::builder()
///     .endpoint_url("http://localhost:9000")
///     .region("us-east-1")
///     .bucket("my-bucket")
///     .key("archive.zip")
///     .build()
///     .await?;
/// # Ok(())
/// # }
/// ```
pub struct S3ZipReaderBuilder {
    client: Option<Client>,
    bucket: String,
    key: String,
    endpoint_url: Option<String>,
    region: Option<String>,
    force_path_style: bool,
}

impl S3ZipReader {
    /// Create a new S3 ZIP reader.
    ///
    /// # Arguments
    ///
    /// * `client` - AWS S3 client
    /// * `bucket` - S3 bucket name
    /// * `key` - S3 object key (path)
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use s_zip::cloud::S3ZipReader;
    /// # use aws_sdk_s3::Client;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = aws_config::load_from_env().await;
    /// let client = Client::new(&config);
    ///
    /// let reader = S3ZipReader::new(
    ///     client,
    ///     "my-bucket",
    ///     "exports/archive.zip"
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(
        client: Client,
        bucket: impl Into<String>,
        key: impl Into<String>,
    ) -> Result<Self> {
        Self::builder()
            .client(client)
            .bucket(bucket)
            .key(key)
            .build()
            .await
    }

    /// Create a builder for configuring the S3 reader.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use s_zip::cloud::S3ZipReader;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Read from MinIO
    /// let reader = S3ZipReader::builder()
    ///     .endpoint_url("http://localhost:9000")
    ///     .bucket("my-bucket")
    ///     .key("archive.zip")
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder() -> S3ZipReaderBuilder {
        S3ZipReaderBuilder {
            client: None,
            bucket: String::new(),
            key: String::new(),
            endpoint_url: None,
            region: None,
            force_path_style: false,
        }
    }

    /// Get the total size of the S3 object.
    pub fn size(&self) -> u64 {
        self.size
    }
}

impl S3ZipReaderBuilder {
    /// Set a pre-configured S3 client.
    pub fn client(mut self, client: Client) -> Self {
        self.client = Some(client);
        self
    }

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

    /// Set the S3 object key (path).
    pub fn key(mut self, key: impl Into<String>) -> Self {
        self.key = key.into();
        self
    }

    /// Set a custom endpoint URL for S3-compatible services.
    ///
    /// Use this for MinIO, Cloudflare R2, DigitalOcean Spaces, etc.
    pub fn endpoint_url(mut self, url: impl Into<String>) -> Self {
        self.endpoint_url = Some(url.into());
        self
    }

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

    /// Force path-style URLs instead of virtual-hosted style.
    pub fn force_path_style(mut self, force: bool) -> Self {
        self.force_path_style = force;
        self
    }

    /// Build the S3 reader.
    pub async fn build(self) -> Result<S3ZipReader> {
        let client = match self.client {
            Some(c) => c,
            None => {
                let mut config_loader = aws_config::from_env();

                if let Some(ref endpoint) = self.endpoint_url {
                    config_loader = config_loader.endpoint_url(endpoint);
                }

                if let Some(ref region) = self.region {
                    config_loader = config_loader.region(aws_config::Region::new(region.clone()));
                }

                let sdk_config = config_loader.load().await;

                let mut s3_config = aws_sdk_s3::config::Builder::from(&sdk_config);

                if self.force_path_style {
                    s3_config = s3_config.force_path_style(true);
                }

                Client::from_conf(s3_config.build())
            }
        };

        // Get object metadata to determine size
        let head = client
            .head_object()
            .bucket(&self.bucket)
            .key(&self.key)
            .send()
            .await
            .map_err(|e| {
                SZipError::Io(io::Error::other(format!(
                    "Failed to get S3 object metadata: {}",
                    e
                )))
            })?;

        let size = head
            .content_length()
            .ok_or_else(|| SZipError::Io(io::Error::other("S3 object has no content length")))?
            as u64;

        Ok(S3ZipReader {
            client,
            bucket: self.bucket,
            key: self.key,
            position: 0,
            size,
            read_future: None,
        })
    }
}

impl AsyncRead for S3ZipReader {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        // If we already have a pending future, poll it
        if let Some(fut) = self.read_future.as_mut() {
            match fut.as_mut().poll(cx) {
                Poll::Ready(Ok(bytes)) => {
                    let n = bytes.len().min(buf.remaining());
                    buf.put_slice(&bytes[..n]);
                    self.position += n as u64;
                    self.read_future = None;
                    return Poll::Ready(Ok(()));
                }
                Poll::Ready(Err(e)) => {
                    self.read_future = None;
                    return Poll::Ready(Err(e));
                }
                Poll::Pending => return Poll::Pending,
            }
        }

        // Calculate byte range to read
        let start = self.position;
        let end = (start + buf.remaining() as u64 - 1).min(self.size - 1);

        if start >= self.size {
            return Poll::Ready(Ok(())); // EOF
        }

        let range = format!("bytes={}-{}", start, end);

        // Create future for reading from S3
        let client = self.client.clone();
        let bucket = self.bucket.clone();
        let key = self.key.clone();

        let fut = Box::pin(async move {
            let response = client
                .get_object()
                .bucket(&bucket)
                .key(&key)
                .range(range)
                .send()
                .await
                .map_err(|e| io::Error::other(format!("S3 GetObject failed: {}", e)))?;

            let bytes = response
                .body
                .collect()
                .await
                .map_err(|e| io::Error::other(format!("Failed to read S3 body: {}", e)))?;

            Ok::<_, io::Error>(bytes.into_bytes().to_vec())
        });

        // Store the future and poll it
        self.read_future = Some(fut);

        // Re-enter poll_read to poll the new future
        self.poll_read(cx, buf)
    }
}

impl AsyncSeek for S3ZipReader {
    fn start_seek(mut self: Pin<&mut Self>, position: io::SeekFrom) -> io::Result<()> {
        let new_pos = match position {
            io::SeekFrom::Start(pos) => pos as i64,
            io::SeekFrom::End(offset) => self.size as i64 + offset,
            io::SeekFrom::Current(offset) => self.position as i64 + offset,
        };

        if new_pos < 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "Invalid seek position",
            ));
        }

        self.position = new_pos as u64;
        Ok(())
    }

    fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
        Poll::Ready(Ok(self.position))
    }
}

impl Unpin for S3ZipReader {}

unsafe impl Send for S3ZipReader {}