Author: Barnaby Keene [accounts@southcla.ws]
Committer: GitHub [noreply@github.com] Fri, 13 Mar 2020 16:07:12 +0000
Hash: 3864a383a16f9f18feee4ae3d4f30e8946054a1d
Timestamp: Fri, 13 Mar 2020 16:07:12 +0000 (4 years ago)

+10 -7 +/-1 browse
Fix custom region logic (#14)
Fix custom region logic (#14)

Previously, this code used Rusoto to get the default region as an object then, if there was a custom endpoint turn that region into a Region::Custom with the previously acquired default region name as the custom name. However, Region::default() performs internal checks to filter out names that do not match the official AWS region names and defaults to 'us-east-1' if it's not in the list. This result in custom region names always being ignored and defaulting to 'us-east-1'. The fix changes the logic around this to first check if a custom endpoint is used and then check for custom region names - if no custom endpoint is specified, it simply uses the AWS default.
1diff --git a/src/storage/s3.rs b/src/storage/s3.rs
2index a421eb2..60d3b00 100644
3--- a/src/storage/s3.rs
4+++ b/src/storage/s3.rs
5 @@ -105,15 +105,18 @@ impl Backend {
6 prefix.pop();
7 }
8
9- // `Region::default` will get try to get the region from the environment
10- // and fallback to a default if it isn't found.
11- let mut region = Region::default();
12- if let Ok(endpoint) = std::env::var("AWS_S3_ENDPOINT") {
13- region = Region::Custom {
14- name: region.name().to_owned(),
15+ // If a custom endpoint is set, do not use the AWS default (us-east-1)
16+ // instead, check environment variables for a region name.
17+ let region = if let Ok(endpoint) = std::env::var("AWS_S3_ENDPOINT") {
18+ Region::Custom {
19+ name: std::env::var("AWS_DEFAULT_REGION")
20+ .or_else(|_| std::env::var("AWS_REGION"))
21+ .unwrap(),
22 endpoint,
23 }
24- }
25+ } else {
26+ Region::default()
27+ };
28
29 Backend::with_client(S3Client::new(region), bucket, prefix)
30 }