Skip to content

Commit 8268445

Browse files
committed
Move some code around
1 parent 2911c7a commit 8268445

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/storage/s3.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use std::io::Read;
1010
use time::Timespec;
1111
use tokio::runtime::Runtime;
1212

13+
pub(crate) static S3_BUCKET_NAME: &str = "rust-docs-rs";
14+
1315
pub(crate) struct S3Backend<'a> {
1416
client: S3Client,
1517
bucket: &'a str,
@@ -100,6 +102,32 @@ fn parse_timespec(raw: &str) -> Result<Timespec, Error> {
100102
Ok(time::strptime(raw, TIME_FMT)?.to_timespec())
101103
}
102104

105+
pub(crate) fn s3_client() -> Option<S3Client> {
106+
// If AWS keys aren't configured, then presume we should use the DB exclusively
107+
// for file storage.
108+
if std::env::var_os("AWS_ACCESS_KEY_ID").is_none() && std::env::var_os("FORCE_S3").is_none() {
109+
return None;
110+
}
111+
let creds = match DefaultCredentialsProvider::new() {
112+
Ok(creds) => creds,
113+
Err(err) => {
114+
warn!("failed to retrieve AWS credentials: {}", err);
115+
return None;
116+
}
117+
};
118+
Some(S3Client::new_with(
119+
rusoto_core::request::HttpClient::new().unwrap(),
120+
creds,
121+
std::env::var("S3_ENDPOINT")
122+
.ok()
123+
.map(|e| Region::Custom {
124+
name: std::env::var("S3_REGION").unwrap_or_else(|_| "us-west-1".to_owned()),
125+
endpoint: e,
126+
})
127+
.unwrap_or(Region::UsWest1),
128+
))
129+
}
130+
103131
#[cfg(test)]
104132
pub(crate) mod tests {
105133
use super::*;
@@ -259,31 +287,3 @@ pub(crate) mod tests {
259287
// NOTE: On s3, it will succeed and create a file called `/`.
260288
// NOTE: On min.io, it will fail with 'Object name contains unsupported characters.'
261289
}
262-
263-
pub(crate) static S3_BUCKET_NAME: &str = "rust-docs-rs";
264-
265-
pub(crate) fn s3_client() -> Option<S3Client> {
266-
// If AWS keys aren't configured, then presume we should use the DB exclusively
267-
// for file storage.
268-
if std::env::var_os("AWS_ACCESS_KEY_ID").is_none() && std::env::var_os("FORCE_S3").is_none() {
269-
return None;
270-
}
271-
let creds = match DefaultCredentialsProvider::new() {
272-
Ok(creds) => creds,
273-
Err(err) => {
274-
warn!("failed to retrieve AWS credentials: {}", err);
275-
return None;
276-
}
277-
};
278-
Some(S3Client::new_with(
279-
rusoto_core::request::HttpClient::new().unwrap(),
280-
creds,
281-
std::env::var("S3_ENDPOINT")
282-
.ok()
283-
.map(|e| Region::Custom {
284-
name: std::env::var("S3_REGION").unwrap_or_else(|_| "us-west-1".to_owned()),
285-
endpoint: e,
286-
})
287-
.unwrap_or(Region::UsWest1),
288-
))
289-
}

0 commit comments

Comments
 (0)