How could I fallback to use credentials from a parsed file (config.yml
) if no environment variables were found? For testing, I am using this example:
extern crate rusoto_core;
extern crate rusoto_s3;
use rusoto_core::credential::ChainProvider;
use rusoto_core::request::HttpClient;
use rusoto_core::Region;
use rusoto_s3::{S3, S3Client};
use std::time::{Duration, Instant};
fn main() {
let mut chain = ChainProvider::new();
chain.set_timeout(Duration::from_millis(200));
let s3client = S3Client::new_with(
HttpClient::new().expect("failed to create request dispatcher"),
chain,
Region::UsEast1,
);
let start = Instant::now();
println!("Starting up at {:?}", start);
match s3client.list_buckets().sync() {
Err(e) => println!("Error listing buckets: {}", e),
Ok(buckets) => println!("Buckets found: {:?}", buckets),
};
println!("Took {:?}", Instant::now().duration_since(start));
}
It works but requires the environment variables AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
. I would like to extend it so that If there are no defined environment variables, I could use the keys found in a parsed file:
// parse config file
let file = std::fs::File::open("config.yml").expect("Unable to open file");
let yml: Config = match serde_yaml::from_reader(file) {
Err(err) => {
println!("Error: {}", err);
return;
}
Ok(yml) => yml,
};
The config.yml
for example could be something like:
---
endpoint: s3.provider
access_key: ACCESS_KEY_ID
secret_key: SECRET_ACCESS_KEY
What could I add to the chain
to use the credentials found in the config.yml
, something probably like:
let config_provider = StaticProvider::new_minimal(yml.access_key, yml.secret_key);
How to give preference to the environment and if not found then use the credentials provided by the StaticProvider
?