brows3 0.2.46

Premium, Secure S3 Browser for macOS, Windows, and Linux.
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
use crate::credentials::{CredentialType, Profile};
use crate::error::{AppError, Result};
use aws_config::Region;
use aws_sdk_s3::config::{RequestChecksumCalculation, ResponseChecksumValidation};
use aws_sdk_s3::Client;
use serde::{Deserialize, Serialize};

use std::collections::{HashMap, VecDeque};

type SortedFolderCacheKey = (String, String, String, String, String);

const MAX_SORTED_CACHE_ENTRIES: usize = 32;
const MAX_SORTED_CACHE_ITEMS: usize = 100_000;

/// Normalize an endpoint URL to ensure it has a scheme.
/// Many S3-compatible providers (Linode, DigitalOcean, etc.) may be configured
/// without a scheme, causing the AWS SDK to fail with "dispatch failure".
pub(crate) fn normalize_endpoint_url(url: &str) -> String {
    let trimmed = url.trim();
    if trimmed.starts_with("http://") || trimmed.starts_with("https://") {
        trimmed.to_string()
    } else {
        format!("https://{}", trimmed)
    }
}

fn build_s3_config(sdk_config: &aws_config::SdkConfig, profile: &Profile) -> aws_sdk_s3::Config {
    let mut builder = aws_sdk_s3::config::Builder::from(sdk_config);

    if let CredentialType::CustomEndpoint { endpoint_url, .. } = &profile.credential_type {
        builder = builder.endpoint_url(normalize_endpoint_url(endpoint_url));
    }

    // A global endpoint_url loaded from an environment variable or shared
    // profile is a custom S3 endpoint just as much as one entered in the UI.
    // Path-style addressing is required by common providers such as MinIO
    // unless they have explicitly configured wildcard bucket DNS.
    if matches!(
        profile.credential_type,
        CredentialType::CustomEndpoint { .. }
    ) || sdk_config.endpoint_url().is_some()
    {
        builder = builder
            .force_path_style(true)
            .response_checksum_validation(ResponseChecksumValidation::WhenRequired);
    }

    builder = builder.request_checksum_calculation(RequestChecksumCalculation::WhenRequired);

    builder.build()
}

/// Build an S3 client for both production operations and connection tests so
/// profile, endpoint, and compatibility behavior cannot drift between them.
pub(crate) async fn load_sdk_config(
    profile: &Profile,
    override_region: Option<String>,
) -> aws_config::SdkConfig {
    let region_str = override_region
        .or_else(|| profile.region.clone())
        .unwrap_or_else(|| "us-east-1".to_string());
    let region = Region::new(region_str);

    match &profile.credential_type {
        CredentialType::Environment => {
            aws_config::defaults(aws_config::BehaviorVersion::latest())
                .region(region)
                .load()
                .await
        }
        CredentialType::SharedConfig { profile_name } => {
            aws_config::defaults(aws_config::BehaviorVersion::latest())
                .region(region)
                .profile_name(profile_name.as_deref().unwrap_or("default"))
                .load()
                .await
        }
        CredentialType::Manual {
            access_key_id,
            secret_access_key,
        } => {
            let creds = aws_credential_types::Credentials::new(
                access_key_id,
                secret_access_key,
                None,
                None,
                "manual",
            );
            aws_config::defaults(aws_config::BehaviorVersion::latest())
                .region(region)
                .credentials_provider(creds)
                .load()
                .await
        }
        CredentialType::CustomEndpoint {
            access_key_id,
            secret_access_key,
            ..
        } => {
            let creds = aws_credential_types::Credentials::new(
                access_key_id,
                secret_access_key,
                None,
                None,
                "custom_endpoint",
            );
            aws_config::defaults(aws_config::BehaviorVersion::latest())
                .region(region)
                .credentials_provider(creds)
                .load()
                .await
        }
    }
}

pub(crate) fn client_from_sdk_config(
    sdk_config: &aws_config::SdkConfig,
    profile: &Profile,
) -> Client {
    Client::from_conf(build_s3_config(sdk_config, profile))
}

pub(crate) async fn build_s3_client(
    profile: &Profile,
    override_region: Option<String>,
) -> Result<Client> {
    let sdk_config = load_sdk_config(profile, override_region).await;
    Ok(client_from_sdk_config(&sdk_config, profile))
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct S3Object {
    pub key: String,
    pub last_modified: Option<String>,
    pub size: i64,
    pub storage_class: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FolderContent {
    pub generation: String,
    pub objects: Vec<S3Object>,
    pub common_prefixes: Vec<String>,
}

/// S3 Client Manager - creates and caches S3 clients per profile and region
pub struct S3ClientManager {
    clients: HashMap<(String, String), Client>,
    sorted_folder_cache: HashMap<SortedFolderCacheKey, FolderContent>,
    sorted_folder_cache_order: VecDeque<SortedFolderCacheKey>,
    sorted_folder_cache_items: usize,
    bucket_regions: HashMap<(String, String), String>, // profile revision + bucket
}

impl S3ClientManager {
    pub fn new() -> Self {
        Self {
            clients: HashMap::new(),
            sorted_folder_cache: HashMap::new(),
            sorted_folder_cache_order: VecDeque::new(),
            sorted_folder_cache_items: 0,
            bucket_regions: HashMap::new(),
        }
    }

    /// Get or create an S3 client for the given profile's default region
    pub async fn get_client(&mut self, profile: &Profile) -> Result<&Client> {
        let region = profile
            .region
            .clone()
            .unwrap_or_else(|| "us-east-1".to_string());
        self.get_client_for_region(profile, &region).await
    }

    /// Get or create an S3 client for the given profile and specific region
    pub async fn get_client_for_region(
        &mut self,
        profile: &Profile,
        region: &str,
    ) -> Result<&Client> {
        let key = (profile.cache_identity(), region.to_string());

        if !self.clients.contains_key(&key) {
            let client = self.build_client(profile, Some(region.to_string())).await?;
            self.clients.insert(key.clone(), client);
        }

        Ok(self.clients.get(&key).unwrap())
    }

    /// Build a new S3 client for the given profile
    async fn build_client(
        &self,
        profile: &Profile,
        override_region: Option<String>,
    ) -> Result<Client> {
        build_s3_client(profile, override_region).await
    }

    /// Clear cached clients, sorted folder results, and discovered regions.
    pub fn clear_cache(&mut self) {
        self.clients.clear();
        self.sorted_folder_cache.clear();
        self.sorted_folder_cache_order.clear();
        self.sorted_folder_cache_items = 0;
        self.bucket_regions.clear();
    }

    /// Get cached region for a bucket
    pub fn get_bucket_region(&self, profile: &Profile, bucket_name: &str) -> Option<String> {
        self.bucket_regions
            .get(&(profile.cache_identity(), bucket_name.to_string()))
            .cloned()
    }

    /// Cache the region for a bucket
    pub fn set_bucket_region(&mut self, profile: &Profile, bucket_name: &str, region: String) {
        self.bucket_regions
            .insert((profile.cache_identity(), bucket_name.to_string()), region);
    }

    /// Cache the same region for a set of buckets.
    pub fn set_bucket_regions<I>(&mut self, profile: &Profile, bucket_names: I, region: &str)
    where
        I: IntoIterator,
        I::Item: AsRef<str>,
    {
        for bucket_name in bucket_names {
            self.set_bucket_region(profile, bucket_name.as_ref(), region.to_string());
        }
    }

    pub fn get_sorted_folder_content(
        &self,
        profile_id: &str,
        bucket_name: &str,
        prefix: &str,
        sort_field: &str,
        sort_direction: &str,
    ) -> Option<&FolderContent> {
        self.sorted_folder_cache.get(&(
            profile_id.to_string(),
            bucket_name.to_string(),
            prefix.to_string(),
            sort_field.to_string(),
            sort_direction.to_string(),
        ))
    }

    pub fn set_sorted_folder_content(
        &mut self,
        profile_id: &str,
        bucket_name: &str,
        prefix: &str,
        sort_field: &str,
        sort_direction: &str,
        content: FolderContent,
    ) {
        let key = (
            profile_id.to_string(),
            bucket_name.to_string(),
            prefix.to_string(),
            sort_field.to_string(),
            sort_direction.to_string(),
        );
        let item_count = content
            .objects
            .len()
            .saturating_add(content.common_prefixes.len());

        if let Some(previous) = self.sorted_folder_cache.remove(&key) {
            self.sorted_folder_cache_items = self.sorted_folder_cache_items.saturating_sub(
                previous
                    .objects
                    .len()
                    .saturating_add(previous.common_prefixes.len()),
            );
            self.sorted_folder_cache_order
                .retain(|cached_key| cached_key != &key);
        }

        if item_count > MAX_SORTED_CACHE_ITEMS {
            return;
        }

        while self.sorted_folder_cache.len() >= MAX_SORTED_CACHE_ENTRIES
            || self.sorted_folder_cache_items.saturating_add(item_count) > MAX_SORTED_CACHE_ITEMS
        {
            let Some(oldest_key) = self.sorted_folder_cache_order.pop_front() else {
                break;
            };
            if let Some(evicted) = self.sorted_folder_cache.remove(&oldest_key) {
                self.sorted_folder_cache_items = self.sorted_folder_cache_items.saturating_sub(
                    evicted
                        .objects
                        .len()
                        .saturating_add(evicted.common_prefixes.len()),
                );
            }
        }

        self.sorted_folder_cache_items = self.sorted_folder_cache_items.saturating_add(item_count);
        self.sorted_folder_cache_order.push_back(key.clone());
        self.sorted_folder_cache.insert(key, content);
    }

    /// Remove cached sorted results for a specific profile and bucket.
    pub fn remove_bucket_cache(&mut self, profile_id: &str, bucket_name: &str) {
        let pid = profile_id.to_string();
        let revision_prefix = format!("{profile_id}:");
        let bname = bucket_name.to_string();

        self.sorted_folder_cache.retain(|(p, b, _, _, _), _| {
            (p != &pid && !p.starts_with(&revision_prefix)) || b != &bname
        });
        self.sorted_folder_cache_order.retain(|(p, b, _, _, _)| {
            (p != &pid && !p.starts_with(&revision_prefix)) || b != &bname
        });
        self.sorted_folder_cache_items = self
            .sorted_folder_cache
            .values()
            .map(|content| {
                content
                    .objects
                    .len()
                    .saturating_add(content.common_prefixes.len())
            })
            .sum();
    }
}

impl Default for S3ClientManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Bucket information returned from S3
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BucketInfo {
    pub name: String,
    pub region: Option<String>,
    pub creation_date: Option<String>,
    pub object_count: Option<u64>,
    pub total_size: Option<u64>,
    pub total_size_formatted: Option<String>,
}

/// List all buckets accessible by the current credentials
pub async fn list_buckets(client: &Client) -> Result<Vec<BucketInfo>> {
    let response = client
        .list_buckets()
        .send()
        .await
        .map_err(|e| AppError::S3Error(e.to_string()))?;

    let buckets = response
        .buckets()
        .iter()
        .map(|b| BucketInfo {
            name: b.name().unwrap_or_default().to_string(),
            region: None, // Will be fetched separately if needed
            creation_date: b.creation_date().map(|d| d.to_string()),
            object_count: None,
            total_size: None,
            total_size_formatted: None,
        })
        .collect();

    Ok(buckets)
}

/// Get the region for a specific bucket.
/// Note: GetBucketLocation is not supported by all S3-compatible providers
/// (e.g., Linode Object Storage, DigitalOcean Spaces). Callers should handle
/// errors gracefully and fall back to the profile's configured region.
pub async fn get_bucket_region(client: &Client, bucket_name: &str) -> Result<String> {
    // Use a timeout to prevent hanging on providers that don't support this API
    let result = tokio::time::timeout(
        std::time::Duration::from_secs(10),
        client.get_bucket_location().bucket(bucket_name).send(),
    )
    .await;

    let response = match result {
        Ok(Ok(resp)) => resp,
        Ok(Err(e)) => {
            return Err(AppError::S3Error(format!(
                "GetBucketLocation failed for '{}': {}",
                bucket_name, e
            )));
        }
        Err(_) => {
            return Err(AppError::S3Error(format!(
                "GetBucketLocation timed out for '{}'",
                bucket_name
            )));
        }
    };

    // Empty string means us-east-1
    let region = response
        .location_constraint()
        .map(|l| l.as_str().to_string())
        .unwrap_or_else(|| "us-east-1".to_string());

    let region = if region.is_empty() {
        "us-east-1".to_string()
    } else {
        region
    };

    Ok(region)
}

/// Format bytes to human-readable size
pub fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;
    const TB: u64 = GB * 1024;

    if bytes >= TB {
        format!("{:.2} TB", bytes as f64 / TB as f64)
    } else if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}

#[cfg(test)]
mod tests {
    use super::{
        build_s3_config, normalize_endpoint_url, FolderContent, S3ClientManager, S3Object,
        MAX_SORTED_CACHE_ENTRIES,
    };
    use crate::credentials::{CredentialType, Profile};
    use aws_config::{Region, SdkConfig};
    use aws_sdk_s3::presigning::PresigningConfig;
    use std::time::Duration;

    #[test]
    fn bucket_regions_are_isolated_by_profile_and_revision() {
        let mut manager = S3ClientManager::new();
        let a = Profile::new("A".into(), CredentialType::Environment, None);
        let b = Profile::new("B".into(), CredentialType::Environment, None);
        manager.set_bucket_region(&a, "shared", "region-a".into());
        manager.set_bucket_region(&b, "shared", "region-b".into());
        assert_eq!(
            manager.get_bucket_region(&a, "shared").as_deref(),
            Some("region-a")
        );
        assert_eq!(
            manager.get_bucket_region(&b, "shared").as_deref(),
            Some("region-b")
        );
        let mut edited = a.clone();
        edited.updated_at = a.updated_at.map(|date| date + chrono::Duration::seconds(1));
        assert!(manager.get_bucket_region(&edited, "shared").is_none());
    }

    #[test]
    fn normalize_endpoint_url_preserves_existing_scheme() {
        assert_eq!(
            normalize_endpoint_url("https://us-east-1.linodeobjects.com"),
            "https://us-east-1.linodeobjects.com"
        );
        assert_eq!(
            normalize_endpoint_url("http://localhost:9000"),
            "http://localhost:9000"
        );
    }

    #[test]
    fn normalize_endpoint_url_defaults_to_https() {
        assert_eq!(
            normalize_endpoint_url("us-east-1.linodeobjects.com"),
            "https://us-east-1.linodeobjects.com"
        );
    }

    #[test]
    fn normalize_endpoint_url_trims_whitespace() {
        assert_eq!(
            normalize_endpoint_url("  us-east-1.linodeobjects.com  "),
            "https://us-east-1.linodeobjects.com"
        );
    }

    #[tokio::test]
    async fn shared_config_endpoint_uses_path_style_addressing() {
        let sdk_config = SdkConfig::builder()
            .behavior_version(aws_config::BehaviorVersion::latest())
            .region(Region::new("us-east-1"))
            .endpoint_url("http://localhost:9100")
            .credentials_provider(
                aws_credential_types::provider::SharedCredentialsProvider::new(
                    aws_credential_types::Credentials::new(
                        "access-key",
                        "secret-key",
                        None,
                        None,
                        "test",
                    ),
                ),
            )
            .build();
        let profile = Profile::new(
            "MinIO shared profile".to_string(),
            CredentialType::SharedConfig {
                profile_name: Some("minio-local".to_string()),
            },
            Some("us-east-1".to_string()),
        );
        let client = aws_sdk_s3::Client::from_conf(build_s3_config(&sdk_config, &profile));

        let request = client
            .get_object()
            .bucket("test-bucket")
            .key("folder/file.txt")
            .presigned(PresigningConfig::expires_in(Duration::from_secs(60)).unwrap())
            .await
            .unwrap();

        assert!(
            request
                .uri()
                .starts_with("http://localhost:9100/test-bucket/folder/file.txt?"),
            "unexpected presigned URI: {}",
            request.uri()
        );
    }

    #[test]
    fn aws_upload_compatibility_preserves_download_checksum_validation() {
        let config = SdkConfig::builder()
            .behavior_version(aws_config::BehaviorVersion::latest())
            .region(Region::new("us-east-1"))
            .response_checksum_validation(
                aws_sdk_s3::config::ResponseChecksumValidation::WhenSupported,
            )
            .build();
        let profile = Profile::new("AWS".into(), CredentialType::Environment, None);
        let result = build_s3_config(&config, &profile);
        assert_eq!(
            result.request_checksum_calculation(),
            Some(&aws_sdk_s3::config::RequestChecksumCalculation::WhenRequired)
        );
        assert_eq!(
            result.response_checksum_validation(),
            Some(&aws_sdk_s3::config::ResponseChecksumValidation::WhenSupported)
        );
    }

    #[test]
    fn sorted_folder_cache_evicts_the_oldest_entry_at_its_limit() {
        let mut manager = S3ClientManager::new();

        for index in 0..=MAX_SORTED_CACHE_ENTRIES {
            manager.set_sorted_folder_content(
                "profile",
                "bucket",
                &format!("prefix-{index}/"),
                "size",
                "desc",
                FolderContent {
                    objects: vec![S3Object {
                        key: format!("object-{index}"),
                        last_modified: None,
                        size: index as i64,
                        storage_class: None,
                    }],
                    common_prefixes: Vec::new(),
                    generation: index.to_string(),
                },
            );
        }

        assert!(manager
            .get_sorted_folder_content("profile", "bucket", "prefix-0/", "size", "desc")
            .is_none());
        assert!(manager
            .get_sorted_folder_content(
                "profile",
                "bucket",
                &format!("prefix-{MAX_SORTED_CACHE_ENTRIES}/"),
                "size",
                "desc"
            )
            .is_some());
        assert_eq!(manager.sorted_folder_cache.len(), MAX_SORTED_CACHE_ENTRIES);
    }
}