Skip to content

Commit

Permalink
Read in the S3 region correctly for rusoto
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed Jul 5, 2019
1 parent f466b93 commit 93c9b9a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/cache/cache.rs
Expand Up @@ -260,11 +260,11 @@ pub fn storage_from_config(config: &Config, pool: &CpuPool) -> Arc<dyn Storage>
}
CacheType::S3(config::S3CacheConfig {
ref bucket,
ref endpoint,
ref region,
}) => {
debug!("Trying S3Cache({}, {})", bucket, endpoint);
debug!("Trying S3Cache({}, {:?})", bucket, region);
#[cfg(feature = "s3")]
match S3Cache::new(&bucket, &endpoint) {
match S3Cache::new(&bucket, region) {
Ok(s) => {
trace!("Using S3Cache");
return Arc::new(s);
Expand Down
4 changes: 2 additions & 2 deletions src/cache/s3.rs
Expand Up @@ -30,8 +30,8 @@ pub struct S3Cache {

impl S3Cache {
/// Create a new `S3Cache` storing data in `bucket`.
pub fn new(bucket: &str, endpoint: &str) -> Result<S3Cache> {
let bucket = Rc::new(Bucket::new(bucket, endpoint)?);
pub fn new(bucket: &str, region: &rusoto_core::Region) -> Result<S3Cache> {
let bucket = Rc::new(Bucket::new(bucket, region)?);
Ok(S3Cache { bucket })
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/config.rs
Expand Up @@ -14,6 +14,7 @@

use directories::ProjectDirs;
use regex::Regex;
use rusoto_core::Region;
#[cfg(any(feature = "dist-client", feature = "dist-server"))]
#[cfg(any(feature = "dist-client", feature = "dist-server"))]
use serde::ser::{Serialize, Serializer};
Expand Down Expand Up @@ -184,15 +185,15 @@ pub struct RedisCacheConfig {
pub url: String,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct S3CacheConfig {
pub bucket: String,
pub endpoint: String,
pub region: Region,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
pub enum CacheType {
Azure(AzureCacheConfig),
GCS(GCSCacheConfig),
Expand Down Expand Up @@ -396,16 +397,17 @@ pub struct EnvConfig {
fn config_from_env() -> EnvConfig {
let s3 = env::var("SCCACHE_BUCKET").ok()
.map(|bucket| {
let endpoint = match env::var("SCCACHE_ENDPOINT") {
Ok(endpoint) => format!("{}/{}", endpoint, bucket),
_ => match env::var("SCCACHE_REGION") {
Ok(ref region) if region != "us-east-1" =>
format!("{}.s3-{}.amazonaws.com", bucket, region),
_ => format!("{}.s3.amazonaws.com", bucket),
let region = match env::var("SCCACHE_ENDPOINT") {
Ok(endpoint) => Region::Custom {
name: Region::default().name().into(),
endpoint: format!("{}/{}", endpoint, bucket),
},
_ => match env::var("SCCACHE_REGION").ok().and_then(|s| s.parse().ok()) {
Some(region) => region,
None => Region::default(),
},
};
S3CacheConfig { bucket, endpoint }
});
S3CacheConfig { bucket, region } });

let redis = env::var("SCCACHE_REDIS").ok()
.map(|url| RedisCacheConfig { url });
Expand Down Expand Up @@ -474,7 +476,7 @@ fn config_from_env() -> EnvConfig {
EnvConfig { cache }
}

#[derive(Debug, Default, PartialEq, Eq)]
#[derive(Debug, Default, PartialEq)]
pub struct Config {
pub caches: Vec<CacheType>,
pub fallback_cache: DiskCacheConfig,
Expand Down
8 changes: 2 additions & 6 deletions src/simples3/s3.rs
Expand Up @@ -7,7 +7,6 @@ use std::fmt;

use futures::{Future, Stream};
use rusoto_s3::S3;
use rusoto_core::Region;

use crate::errors::*;

Expand All @@ -24,13 +23,10 @@ impl fmt::Display for Bucket {
}

impl Bucket {
pub fn new(bucket_name: &str, endpoint: &str) -> Result<Bucket> {
pub fn new(bucket_name: &str, region: &rusoto_core::Region) -> Result<Bucket> {
Ok(Bucket {
bucket_name: bucket_name.to_owned(),
client: rusoto_s3::S3Client::new(rusoto_core::Region::Custom {
name: Region::default().name().into(),
endpoint: endpoint.into(),
}),
client: rusoto_s3::S3Client::new(region.clone()),
})
}

Expand Down

0 comments on commit 93c9b9a

Please sign in to comment.