Add S3 bucket configuration

This commit is contained in:
2023-08-05 14:49:17 +02:00
parent aa351bfae9
commit 4cd7519890
6 changed files with 400 additions and 8 deletions

View File

@ -1,4 +1,6 @@
use clap::Parser;
use s3::creds::Credentials;
use s3::{Bucket, Region};
/// GeneIT backend API
#[derive(Parser, Debug, Clone)]
@ -115,6 +117,30 @@ pub struct AppConfig {
/// OpenID login redirect URL
#[arg(long, env, default_value = "APP_ORIGIN/oidc_cb")]
oidc_redirect_url: String,
/// S3 Bucket name
#[arg(long, env, default_value = "geneit-data")]
s3_bucket_name: String,
/// S3 region (if not using Minio)
#[arg(long, env, default_value = "eu-central-1")]
s3_region: String,
/// S3 API endpoint
#[arg(long, env, default_value = "http://localhost:9000")]
s3_endpoint: String,
/// S3 access key
#[arg(long, env, default_value = "topsecret")]
s3_access_key: String,
/// S3 secret key
#[arg(long, env, default_value = "topsecret")]
s3_secret_key: String,
/// S3 skip auto create bucket if not existing
#[arg(long, env)]
pub s3_skip_auto_create_bucket: bool,
}
lazy_static::lazy_static! {
@ -183,6 +209,30 @@ impl AppConfig {
self.oidc_redirect_url
.replace("APP_ORIGIN", &self.website_origin)
}
/// Get s3 credentials
pub fn s3_credentials(&self) -> anyhow::Result<Credentials> {
Ok(Credentials::new(
Some(&self.s3_access_key),
Some(&self.s3_secret_key),
None,
None,
None,
)?)
}
/// Get S3 bucket
pub fn s3_bucket(&self) -> anyhow::Result<Bucket> {
Ok(Bucket::new(
&self.s3_bucket_name,
Region::Custom {
region: self.s3_region.to_string(),
endpoint: self.s3_endpoint.to_string(),
},
self.s3_credentials()?,
)?
.with_path_style())
}
}
#[derive(Debug, Clone, serde::Serialize)]