Skip to content

Commit

Permalink
Refactor FileStore constructor (#916)
Browse files Browse the repository at this point in the history
* Add feature=local support to FileStore::new
  • Loading branch information
kurotych authored Dec 16, 2024
1 parent 52b1aa7 commit 1827835
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions file_store/src/file_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,23 @@ pub struct FileData {

impl FileStore {
pub async fn from_settings(settings: &Settings) -> Result<Self> {
let endpoint: Option<Endpoint> = match &settings.endpoint {
Some(endpoint) => Uri::from_str(endpoint)
.map(Endpoint::immutable)
.map(Some)
.map_err(DecodeError::from)?,
_ => None,
};
let region = Region::new(settings.region.clone());
let region_provider = RegionProviderChain::first_try(region).or_default_provider();

let mut config = aws_config::from_env().region(region_provider);
if let Some(endpoint) = endpoint {
config = config.endpoint_resolver(endpoint);
}

#[cfg(feature = "local")]
if settings.access_key_id.is_some() && settings.secret_access_key.is_some() {
let creds = aws_types::credentials::Credentials::from_keys(
settings.access_key_id.as_ref().unwrap(),
settings.secret_access_key.as_ref().unwrap(),
None,
);
config = config.credentials_provider(creds);
}

let config = config.load().await;

let client = Client::new(&config);
Ok(Self {
client,
bucket: settings.bucket.clone(),
})
let Settings {
bucket,
endpoint,
access_key_id,
secret_access_key,
region,
} = settings.clone();
Self::new(
bucket,
endpoint,
Some(region),
None,
None,
access_key_id,
secret_access_key,
)
.await
}

pub async fn new(
Expand All @@ -65,6 +50,8 @@ impl FileStore {
region: Option<String>,
timeout_config: Option<TimeoutConfig>,
retry_config: Option<RetryConfig>,
_access_key_id: Option<String>,
_secret_access_key: Option<String>,
) -> Result<Self> {
let endpoint: Option<Endpoint> = match &endpoint {
Some(endpoint) => Uri::from_str(endpoint)
Expand All @@ -81,6 +68,16 @@ impl FileStore {
config = config.endpoint_resolver(endpoint);
}

#[cfg(feature = "local")]
if _access_key_id.is_some() && _secret_access_key.is_some() {
let creds = aws_types::credentials::Credentials::from_keys(
_access_key_id.as_ref().unwrap(),
_secret_access_key.as_ref().unwrap(),
None,
);
config = config.credentials_provider(creds);
}

if let Some(timeout) = timeout_config {
config = config.timeout_config(timeout);
}
Expand Down

0 comments on commit 1827835

Please sign in to comment.