Skip to content

Commit 05ea13b

Browse files
pietroalbiniJoshua Nelson
authored and
Joshua Nelson
committed
storage: move S3_REGION and S3_ENDPOINT to Config
1 parent 90dc85b commit 05ea13b

File tree

3 files changed

+23
-39
lines changed

3 files changed

+23
-39
lines changed

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::storage::StorageBackendKind;
22
use failure::{bail, format_err, Error, Fail, ResultExt};
3+
use rusoto_core::Region;
34
use std::env::VarError;
45
use std::path::PathBuf;
56
use std::str::FromStr;
@@ -22,6 +23,8 @@ pub struct Config {
2223

2324
// S3 params
2425
pub(crate) s3_bucket: String,
26+
pub(crate) s3_region: Region,
27+
pub(crate) s3_endpoint: Option<String>,
2528
#[cfg(test)]
2629
pub(crate) s3_bucket_is_temporary: bool,
2730

@@ -55,6 +58,8 @@ impl Config {
5558
storage_backend: env("DOCSRS_STORAGE_BACKEND", StorageBackendKind::Database)?,
5659

5760
s3_bucket: env("DOCSRS_S3_BUCKET", "rust-docs-rs".to_string())?,
61+
s3_region: env("S3_REGION", Region::UsWest1)?,
62+
s3_endpoint: maybe_env("S3_ENDPOINT")?,
5863
// DO NOT CONFIGURE THIS THROUGH AN ENVIRONMENT VARIABLE!
5964
// Accidentally turning this on outside of the test suite might cause data loss in the
6065
// production environment.

src/storage/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ impl Storage {
105105
StorageBackendKind::Database => {
106106
StorageBackend::Database(DatabaseBackend::new(pool, metrics))
107107
}
108-
StorageBackendKind::S3 => StorageBackend::S3(Box::new(S3Backend::new(
109-
s3::s3_client().unwrap(),
110-
metrics,
111-
config,
112-
)?)),
108+
StorageBackendKind::S3 => {
109+
StorageBackend::S3(Box::new(S3Backend::new(metrics, config)?))
110+
}
113111
},
114112
})
115113
}

src/storage/s3.rs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use futures_util::{
66
future::TryFutureExt,
77
stream::{FuturesUnordered, StreamExt},
88
};
9-
use log::warn;
109
use rusoto_core::{region::Region, RusotoError};
1110
use rusoto_credential::DefaultCredentialsProvider;
1211
use rusoto_s3::{
@@ -26,13 +25,23 @@ pub(super) struct S3Backend {
2625
}
2726

2827
impl S3Backend {
29-
pub(super) fn new(
30-
client: S3Client,
31-
metrics: Arc<Metrics>,
32-
config: &Config,
33-
) -> Result<Self, Error> {
28+
pub(super) fn new(metrics: Arc<Metrics>, config: &Config) -> Result<Self, Error> {
3429
let runtime = Runtime::new()?;
3530

31+
// Connect to S3
32+
let client = S3Client::new_with(
33+
rusoto_core::request::HttpClient::new()?,
34+
DefaultCredentialsProvider::new()?,
35+
config
36+
.s3_endpoint
37+
.as_deref()
38+
.map(|endpoint| Region::Custom {
39+
name: config.s3_region.name().to_string(),
40+
endpoint: endpoint.to_string(),
41+
})
42+
.unwrap_or(config.s3_region.clone()),
43+
);
44+
3645
#[cfg(test)]
3746
{
3847
// Create the temporary S3 bucket during tests.
@@ -275,34 +284,6 @@ fn parse_timespec(mut raw: &str) -> Result<DateTime<Utc>, Error> {
275284
))
276285
}
277286

278-
pub(super) fn s3_client() -> Option<S3Client> {
279-
// If AWS keys aren't configured, then presume we should use the DB exclusively
280-
// for file storage.
281-
if std::env::var_os("AWS_ACCESS_KEY_ID").is_none() && std::env::var_os("FORCE_S3").is_none() {
282-
return None;
283-
}
284-
285-
let creds = match DefaultCredentialsProvider::new() {
286-
Ok(creds) => creds,
287-
Err(err) => {
288-
warn!("failed to retrieve AWS credentials: {}", err);
289-
return None;
290-
}
291-
};
292-
293-
Some(S3Client::new_with(
294-
rusoto_core::request::HttpClient::new().unwrap(),
295-
creds,
296-
std::env::var("S3_ENDPOINT")
297-
.ok()
298-
.map(|e| Region::Custom {
299-
name: std::env::var("S3_REGION").unwrap_or_else(|_| "us-west-1".to_owned()),
300-
endpoint: e,
301-
})
302-
.unwrap_or(Region::UsWest1),
303-
))
304-
}
305-
306287
#[cfg(test)]
307288
mod tests {
308289
use super::*;

0 commit comments

Comments
 (0)