rs3gw 0.2.1

High-Performance AI/HPC Object Storage Gateway powered by scirs2-io
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
// S3 Migration Tool for rs3gw
// Migrate data between S3-compatible storage systems

use anyhow::{Context, Result};
use aws_config::BehaviorVersion;
use aws_sdk_s3::{config::Region, Client as S3Client};
use clap::{Parser, Subcommand};
use futures::StreamExt;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::sync::Arc;
use tokio::sync::Semaphore;

#[derive(Parser)]
#[clap(name = "s3-migrate")]
#[clap(about = "Migrate data between S3-compatible storage systems", long_about = None)]
struct Cli {
    #[clap(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Copy objects from source to destination
    Copy {
        /// Source endpoint URL
        #[clap(long, value_parser)]
        source_endpoint: String,

        /// Source access key
        #[clap(long, value_parser)]
        source_access_key: String,

        /// Source secret key
        #[clap(long, value_parser)]
        source_secret_key: String,

        /// Source region
        #[clap(long, default_value = "us-east-1")]
        source_region: String,

        /// Source bucket
        #[clap(long, value_parser)]
        source_bucket: String,

        /// Destination endpoint URL
        #[clap(long, value_parser)]
        dest_endpoint: String,

        /// Destination access key
        #[clap(long, value_parser)]
        dest_access_key: String,

        /// Destination secret key
        #[clap(long, value_parser)]
        dest_secret_key: String,

        /// Destination region
        #[clap(long, default_value = "us-east-1")]
        dest_region: String,

        /// Destination bucket
        #[clap(long, value_parser)]
        dest_bucket: String,

        /// Prefix filter (only migrate objects with this prefix)
        #[clap(long)]
        prefix: Option<String>,

        /// Number of concurrent transfers
        #[clap(long, default_value = "10")]
        concurrency: usize,

        /// Dry run (list objects without copying)
        #[clap(long)]
        dry_run: bool,
    },

    /// Sync objects from source to destination (incremental)
    Sync {
        /// Source endpoint URL
        #[clap(long, value_parser)]
        source_endpoint: String,

        /// Source access key
        #[clap(long, value_parser)]
        source_access_key: String,

        /// Source secret key
        #[clap(long, value_parser)]
        source_secret_key: String,

        /// Source region
        #[clap(long, default_value = "us-east-1")]
        source_region: String,

        /// Source bucket
        #[clap(long, value_parser)]
        source_bucket: String,

        /// Destination endpoint URL
        #[clap(long, value_parser)]
        dest_endpoint: String,

        /// Destination access key
        #[clap(long, value_parser)]
        dest_access_key: String,

        /// Destination secret key
        #[clap(long, value_parser)]
        dest_secret_key: String,

        /// Destination region
        #[clap(long, default_value = "us-east-1")]
        dest_region: String,

        /// Destination bucket
        #[clap(long, value_parser)]
        dest_bucket: String,

        /// Prefix filter
        #[clap(long)]
        prefix: Option<String>,

        /// Number of concurrent transfers
        #[clap(long, default_value = "10")]
        concurrency: usize,

        /// Delete objects from destination that don't exist in source
        #[clap(long)]
        delete: bool,
    },

    /// Verify that source and destination match
    Verify {
        /// Source endpoint URL
        #[clap(long, value_parser)]
        source_endpoint: String,

        /// Source access key
        #[clap(long, value_parser)]
        source_access_key: String,

        /// Source secret key
        #[clap(long, value_parser)]
        source_secret_key: String,

        /// Source region
        #[clap(long, default_value = "us-east-1")]
        source_region: String,

        /// Source bucket
        #[clap(long, value_parser)]
        source_bucket: String,

        /// Destination endpoint URL
        #[clap(long, value_parser)]
        dest_endpoint: String,

        /// Destination access key
        #[clap(long, value_parser)]
        dest_access_key: String,

        /// Destination secret key
        #[clap(long, value_parser)]
        dest_secret_key: String,

        /// Destination region
        #[clap(long, default_value = "us-east-1")]
        dest_region: String,

        /// Destination bucket
        #[clap(long, value_parser)]
        dest_bucket: String,

        /// Prefix filter
        #[clap(long)]
        prefix: Option<String>,
    },
}

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize tracing
    tracing_subscriber::fmt::init();

    let cli = Cli::parse();

    match cli.command {
        Commands::Copy {
            source_endpoint,
            source_access_key,
            source_secret_key,
            source_region,
            source_bucket,
            dest_endpoint,
            dest_access_key,
            dest_secret_key,
            dest_region,
            dest_bucket,
            prefix,
            concurrency,
            dry_run,
        } => {
            let source_client = create_s3_client(
                &source_endpoint,
                &source_access_key,
                &source_secret_key,
                &source_region,
            )
            .await?;

            let dest_client = create_s3_client(
                &dest_endpoint,
                &dest_access_key,
                &dest_secret_key,
                &dest_region,
            )
            .await?;

            copy_objects(
                source_client,
                &source_bucket,
                dest_client,
                &dest_bucket,
                prefix.as_deref(),
                concurrency,
                dry_run,
            )
            .await?;
        }

        Commands::Sync {
            source_endpoint,
            source_access_key,
            source_secret_key,
            source_region,
            source_bucket,
            dest_endpoint,
            dest_access_key,
            dest_secret_key,
            dest_region,
            dest_bucket,
            prefix,
            concurrency,
            delete,
        } => {
            let source_client = create_s3_client(
                &source_endpoint,
                &source_access_key,
                &source_secret_key,
                &source_region,
            )
            .await?;

            let dest_client = create_s3_client(
                &dest_endpoint,
                &dest_access_key,
                &dest_secret_key,
                &dest_region,
            )
            .await?;

            sync_objects(
                source_client,
                &source_bucket,
                dest_client,
                &dest_bucket,
                prefix.as_deref(),
                concurrency,
                delete,
            )
            .await?;
        }

        Commands::Verify {
            source_endpoint,
            source_access_key,
            source_secret_key,
            source_region,
            source_bucket,
            dest_endpoint,
            dest_access_key,
            dest_secret_key,
            dest_region,
            dest_bucket,
            prefix,
        } => {
            let source_client = create_s3_client(
                &source_endpoint,
                &source_access_key,
                &source_secret_key,
                &source_region,
            )
            .await?;

            let dest_client = create_s3_client(
                &dest_endpoint,
                &dest_access_key,
                &dest_secret_key,
                &dest_region,
            )
            .await?;

            verify_objects(
                source_client,
                &source_bucket,
                dest_client,
                &dest_bucket,
                prefix.as_deref(),
            )
            .await?;
        }
    }

    Ok(())
}

async fn create_s3_client(
    endpoint: &str,
    access_key: &str,
    secret_key: &str,
    region: &str,
) -> Result<S3Client> {
    let creds =
        aws_sdk_s3::config::Credentials::new(access_key, secret_key, None, None, "s3-migrate");

    let config = aws_config::defaults(BehaviorVersion::latest())
        .region(Region::new(region.to_string()))
        .credentials_provider(creds)
        .endpoint_url(endpoint)
        .load()
        .await;

    let s3_config = aws_sdk_s3::config::Builder::from(&config)
        .force_path_style(true)
        .build();

    Ok(S3Client::from_conf(s3_config))
}

async fn copy_objects(
    source_client: S3Client,
    source_bucket: &str,
    dest_client: S3Client,
    dest_bucket: &str,
    prefix: Option<&str>,
    concurrency: usize,
    dry_run: bool,
) -> Result<()> {
    println!(
        "Copying objects from {} to {}...",
        source_bucket, dest_bucket
    );
    if dry_run {
        println!("DRY RUN - No objects will be copied");
    }

    // List all objects in source bucket
    let mut continuation_token: Option<String> = None;
    let mut all_objects = Vec::new();

    loop {
        let mut request = source_client
            .list_objects_v2()
            .bucket(source_bucket)
            .max_keys(1000);

        if let Some(prefix) = prefix {
            request = request.prefix(prefix);
        }

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

        let response = request
            .send()
            .await
            .context("Failed to list source objects")?;

        let is_truncated = response.is_truncated() == Some(true);
        let next_token = response.next_continuation_token.clone();

        if let Some(contents) = response.contents {
            all_objects.extend(contents);
        }

        if is_truncated {
            continuation_token = next_token;
        } else {
            break;
        }
    }

    println!("Found {} objects to copy", all_objects.len());

    if dry_run {
        for obj in &all_objects {
            if let Some(key) = &obj.key {
                println!("  {}", key);
            }
        }
        return Ok(());
    }

    // Create progress bar
    let multi_progress = MultiProgress::new();
    let overall_pb = multi_progress.add(ProgressBar::new(all_objects.len() as u64));
    overall_pb.set_style(
        ProgressStyle::default_bar()
            .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos}/{len} ({eta})")
            .context("Failed to create progress style")?,
    );

    // Copy objects with concurrency control
    let semaphore = Arc::new(Semaphore::new(concurrency));
    let source_client = Arc::new(source_client);
    let dest_client = Arc::new(dest_client);
    let source_bucket = Arc::new(source_bucket.to_string());
    let dest_bucket = Arc::new(dest_bucket.to_string());

    let tasks = futures::stream::iter(all_objects.iter().map(|obj| {
        let key = obj.key.clone().unwrap_or_default();
        let source_client = source_client.clone();
        let dest_client = dest_client.clone();
        let source_bucket = source_bucket.clone();
        let dest_bucket = dest_bucket.clone();
        let semaphore = semaphore.clone();
        let pb = overall_pb.clone();

        async move {
            let _permit = semaphore
                .acquire()
                .await
                .map_err(|e| anyhow::anyhow!("Semaphore error: {}", e))?;

            // Get object from source
            let get_response = source_client
                .get_object()
                .bucket(source_bucket.as_str())
                .key(&key)
                .send()
                .await
                .context(format!("Failed to get object: {}", key))?;

            let body = get_response
                .body
                .collect()
                .await
                .context(format!("Failed to read object body: {}", key))?;

            // Put object to destination
            dest_client
                .put_object()
                .bucket(dest_bucket.as_str())
                .key(&key)
                .body(body.into_bytes().into())
                .send()
                .await
                .context(format!("Failed to put object: {}", key))?;

            pb.inc(1);
            Result::<()>::Ok(())
        }
    }))
    .buffer_unordered(concurrency)
    .collect::<Vec<_>>();

    let results = tasks.await;
    overall_pb.finish_with_message("Done");

    // Report errors
    let errors: Vec<_> = results.into_iter().filter_map(|r| r.err()).collect();
    if !errors.is_empty() {
        println!("\nErrors occurred during migration:");
        for error in &errors {
            println!("  {}", error);
        }
        anyhow::bail!("{} errors occurred", errors.len());
    }

    println!("\nMigration complete!");
    Ok(())
}

async fn sync_objects(
    source_client: S3Client,
    source_bucket: &str,
    dest_client: S3Client,
    dest_bucket: &str,
    prefix: Option<&str>,
    concurrency: usize,
    delete: bool,
) -> Result<()> {
    println!(
        "Syncing objects from {} to {}...",
        source_bucket, dest_bucket
    );

    // List source objects
    let source_objects = list_all_objects(&source_client, source_bucket, prefix).await?;
    let dest_objects = list_all_objects(&dest_client, dest_bucket, prefix).await?;

    // Build destination object map for quick lookup
    let mut dest_map = std::collections::HashMap::new();
    for obj in &dest_objects {
        if let Some(key) = &obj.key {
            dest_map.insert(key.clone(), obj);
        }
    }

    // Find objects to copy (new or modified)
    let mut objects_to_copy = Vec::new();
    for obj in &source_objects {
        if let Some(key) = &obj.key {
            let should_copy = match dest_map.get(key) {
                None => true, // Object doesn't exist in destination
                Some(dest_obj) => {
                    // Compare ETags
                    obj.e_tag != dest_obj.e_tag
                }
            };

            if should_copy {
                objects_to_copy.push(obj.clone());
            }
        }
    }

    println!("Found {} objects to copy", objects_to_copy.len());

    // Copy objects
    if !objects_to_copy.is_empty() {
        // Reuse copy logic
        let semaphore = Arc::new(Semaphore::new(concurrency));
        let source_client = Arc::new(source_client);
        let dest_client_arc = Arc::new(dest_client.clone());
        let source_bucket = Arc::new(source_bucket.to_string());
        let dest_bucket_arc = Arc::new(dest_bucket.to_string());

        let tasks = futures::stream::iter(objects_to_copy.iter().map(|obj| {
            let key = obj.key.clone().unwrap_or_default();
            let source_client = source_client.clone();
            let dest_client = dest_client_arc.clone();
            let source_bucket = source_bucket.clone();
            let dest_bucket = dest_bucket_arc.clone();
            let semaphore = semaphore.clone();

            async move {
                let _permit = semaphore
                    .acquire()
                    .await
                    .map_err(|e| anyhow::anyhow!("Semaphore error: {}", e))?;

                let get_response = source_client
                    .get_object()
                    .bucket(source_bucket.as_str())
                    .key(&key)
                    .send()
                    .await
                    .context(format!("Failed to get object: {}", key))?;

                let body = get_response
                    .body
                    .collect()
                    .await
                    .context(format!("Failed to read object body: {}", key))?;

                dest_client
                    .put_object()
                    .bucket(dest_bucket.as_str())
                    .key(&key)
                    .body(body.into_bytes().into())
                    .send()
                    .await
                    .context(format!("Failed to put object: {}", key))?;

                println!("  Copied: {}", key);
                Result::<()>::Ok(())
            }
        }))
        .buffer_unordered(concurrency)
        .collect::<Vec<_>>();

        let results = tasks.await;
        let errors: Vec<_> = results.into_iter().filter_map(|r| r.err()).collect();
        if !errors.is_empty() {
            println!("\nErrors occurred during sync:");
            for error in &errors {
                println!("  {}", error);
            }
            anyhow::bail!("{} errors occurred", errors.len());
        }
    }

    // Handle deletions
    if delete {
        let source_keys: std::collections::HashSet<_> = source_objects
            .iter()
            .filter_map(|obj| obj.key.clone())
            .collect();

        let mut objects_to_delete = Vec::new();
        for obj in &dest_objects {
            if let Some(key) = &obj.key {
                if !source_keys.contains(key) {
                    objects_to_delete.push(key.clone());
                }
            }
        }

        if !objects_to_delete.is_empty() {
            println!(
                "Deleting {} objects from destination",
                objects_to_delete.len()
            );
            for key in objects_to_delete {
                dest_client
                    .delete_object()
                    .bucket(dest_bucket)
                    .key(&key)
                    .send()
                    .await
                    .context(format!("Failed to delete object: {}", key))?;
                println!("  Deleted: {}", key);
            }
        }
    }

    println!("\nSync complete!");
    Ok(())
}

async fn verify_objects(
    source_client: S3Client,
    source_bucket: &str,
    dest_client: S3Client,
    dest_bucket: &str,
    prefix: Option<&str>,
) -> Result<()> {
    println!(
        "Verifying objects between {} and {}...",
        source_bucket, dest_bucket
    );

    let source_objects = list_all_objects(&source_client, source_bucket, prefix).await?;
    let dest_objects = list_all_objects(&dest_client, dest_bucket, prefix).await?;

    let mut dest_map = std::collections::HashMap::new();
    for obj in &dest_objects {
        if let Some(key) = &obj.key {
            dest_map.insert(key.clone(), obj);
        }
    }

    let mut missing = Vec::new();
    let mut mismatch = Vec::new();
    let mut matched = 0;

    for obj in &source_objects {
        if let Some(key) = &obj.key {
            match dest_map.get(key) {
                None => missing.push(key.clone()),
                Some(dest_obj) => {
                    if obj.e_tag != dest_obj.e_tag {
                        mismatch.push(key.clone());
                    } else {
                        matched += 1;
                    }
                }
            }
        }
    }

    println!("\nVerification Results:");
    println!("  Matched: {}", matched);
    println!("  Missing in destination: {}", missing.len());
    println!("  ETag mismatch: {}", mismatch.len());

    if !missing.is_empty() {
        println!("\nMissing objects:");
        for key in &missing {
            println!("  {}", key);
        }
    }

    if !mismatch.is_empty() {
        println!("\nMismatched objects:");
        for key in &mismatch {
            println!("  {}", key);
        }
    }

    if missing.is_empty() && mismatch.is_empty() {
        println!("\n✓ All objects match!");
        Ok(())
    } else {
        anyhow::bail!(
            "Verification failed: {} missing, {} mismatched",
            missing.len(),
            mismatch.len()
        )
    }
}

async fn list_all_objects(
    client: &S3Client,
    bucket: &str,
    prefix: Option<&str>,
) -> Result<Vec<aws_sdk_s3::types::Object>> {
    let mut continuation_token: Option<String> = None;
    let mut all_objects = Vec::new();

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

        if let Some(prefix) = prefix {
            request = request.prefix(prefix);
        }

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

        let response = request
            .send()
            .await
            .context(format!("Failed to list objects in bucket: {}", bucket))?;

        let is_truncated = response.is_truncated() == Some(true);
        let next_token = response.next_continuation_token.clone();

        if let Some(contents) = response.contents {
            all_objects.extend(contents);
        }

        if is_truncated {
            continuation_token = next_token;
        } else {
            break;
        }
    }

    Ok(all_objects)
}