Add S3 bucket configuration
This commit is contained in:
@ -2,3 +2,4 @@
|
||||
|
||||
pub mod db_connection;
|
||||
pub mod redis_connection;
|
||||
pub mod s3_connection;
|
||||
|
47
geneit_backend/src/connections/s3_connection.rs
Normal file
47
geneit_backend/src/connections/s3_connection.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use s3::error::S3Error;
|
||||
use s3::{Bucket, BucketConfiguration};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum BucketServiceError {
|
||||
#[error("Failed to fetch bucket information!")]
|
||||
FailedFetchBucketInfo,
|
||||
}
|
||||
|
||||
/// Create S3 bucket if required
|
||||
pub async fn create_bucket_if_required() -> anyhow::Result<()> {
|
||||
if AppConfig::get().s3_skip_auto_create_bucket {
|
||||
log::debug!("Skipping bucket existence check");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let bucket = AppConfig::get().s3_bucket()?;
|
||||
|
||||
match bucket.location().await {
|
||||
Ok(_) => {
|
||||
log::debug!("The bucket already exists.");
|
||||
return Ok(());
|
||||
}
|
||||
Err(S3Error::Http(404, s)) if s.contains("<Code>NoSuchKey</Code>") => {
|
||||
log::warn!("Failed to fetch bucket location, but it seems that bucket exists.");
|
||||
return Ok(());
|
||||
}
|
||||
Err(S3Error::Http(404, s)) if s.contains("<Code>NoSuchBucket</Code>") => {
|
||||
log::warn!("The bucket does not seem to exists, trying to create it!")
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Got unexpected error when querying bucket info: {}", e);
|
||||
return Err(BucketServiceError::FailedFetchBucketInfo.into());
|
||||
}
|
||||
}
|
||||
|
||||
Bucket::create_with_path_style(
|
||||
&bucket.name,
|
||||
bucket.region,
|
||||
AppConfig::get().s3_credentials()?,
|
||||
BucketConfiguration::private(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user