Switched s3 crates

This commit is contained in:
Benjamin Sherriff
2023-10-21 16:39:01 -04:00
parent 379de4bdb5
commit 8b4d4e1b1f
5 changed files with 56 additions and 42 deletions

View File

@@ -29,7 +29,6 @@ jsonwebtoken = "9.0.0"
redis = { version = "0.23.3", features = ["tokio-comp", "connection-manager", "r2d2"] } redis = { version = "0.23.3", features = ["tokio-comp", "connection-manager", "r2d2"] }
base64 = "0.21.4" base64 = "0.21.4"
rust-s3 = "0.33.0" rust-s3 = "0.33.0"
minio = "0.1.0"
[dependencies.tokio] [dependencies.tokio]
version = "1.32.0" version = "1.32.0"

View File

@@ -124,12 +124,6 @@ impl From<s3::creds::error::CredentialsError> for ServiceError {
} }
} }
impl From<minio::s3::error::Error> for ServiceError {
fn from(error: minio::s3::error::Error) -> ServiceError {
ServiceError::new(500, format!("Unknown minio error: {}", error))
}
}
impl ResponseError for ServiceError { impl ResponseError for ServiceError {
fn error_response(&self) -> HttpResponse { fn error_response(&self) -> HttpResponse {
let status_code = match StatusCode::from_u16(self.status) { let status_code = match StatusCode::from_u16(self.status) {

View File

@@ -27,7 +27,7 @@ mod storage;
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
dotenv().ok(); dotenv().ok();
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,siren=info")); env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,siren=info"));
storage::init(); storage::init().await;
match env::var("DATA_DIR_PATH") { match env::var("DATA_DIR_PATH") {
Ok(data_dir_path) => dnd::load_data(&data_dir_path), Ok(data_dir_path) => dnd::load_data(&data_dir_path),
Err(err) => warn!("Unable to load initial database data: {}", err) Err(err) => warn!("Unable to load initial database data: {}", err)
@@ -111,10 +111,8 @@ async fn main() -> std::io::Result<()> {
let host = env::var("SERVICE_HOST").unwrap_or("localhost".to_string()); let host = env::var("SERVICE_HOST").unwrap_or("localhost".to_string());
let port = env::var("SERVICE_PORT").unwrap_or("5000".to_string()); let port = env::var("SERVICE_PORT").unwrap_or("5000".to_string());
match crate::storage::create_bucket("siren").await { crate::storage::upload_file("test.txt", b"Test").await.unwrap();
Ok(_) => {}, crate::storage::delete_file("test.txt").await.unwrap();
Err(err) => {}
};
let server = match HttpServer::new(move || { let server = match HttpServer::new(move || {
let cors = Cors::default() let cors = Cors::default()

View File

@@ -1,10 +1,10 @@
use diesel::{r2d2::ConnectionManager as DieselConnectionManager, PgConnection}; use diesel::{r2d2::ConnectionManager as DieselConnectionManager, PgConnection};
use minio::s3::{client::Client as MinioClient, http::BaseUrl, creds::StaticProvider, args::{MakeBucketArgs, BucketExistsArgs}};
use redis::{Client as RedisClient, aio::Connection as RedisConnection}; use redis::{Client as RedisClient, aio::Connection as RedisConnection};
use s3::{Region, creds::Credentials, Bucket, BucketConfiguration, request::ResponseData};
use siren::ServiceError; use siren::ServiceError;
use crate::diesel_migrations::MigrationHarness; use crate::diesel_migrations::MigrationHarness;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::{error, info}; use log::{error, info, warn};
use r2d2; use r2d2;
use std::env; use std::env;
@@ -34,33 +34,35 @@ lazy_static! {
let url = format!("redis://{}:{}", host, port); let url = format!("redis://{}:{}", host, port);
RedisClient::open(url).expect("Failed to create redis client") RedisClient::open(url).expect("Failed to create redis client")
}; };
static ref MINIO: MinioClient = { static ref BUCKET: Bucket = {
let url = env::var("MINIO_URL").unwrap_or("localhost".to_string()); let url = env::var("MINIO_URL").unwrap_or("localhost".to_string());
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string()); let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
let base_url = format!("http://{}:{}", url, port).parse::<BaseUrl>().unwrap();
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set"); let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set"); let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
let base_url = format!("http://{}:{}", url, port);
let static_provider = StaticProvider::new( let region = Region::Custom {
&user, region: "".to_string(),
&password, endpoint: base_url,
None };
);
MinioClient::new( let credentials = Credentials {
base_url, access_key: Some(user),
Some(Box::new(static_provider)), secret_key: Some(password),
None, security_token: None,
None session_token: None,
).expect("Failed to create minio client") expiration: None
};
Bucket::new("siren", region.clone(), credentials.clone()).expect("Failed to create S3 Bucket").with_path_style()
}; };
} }
pub fn init() { pub async fn init() {
lazy_static::initialize(&POOL); lazy_static::initialize(&POOL);
lazy_static::initialize(&REDIS); lazy_static::initialize(&REDIS);
lazy_static::initialize(&MINIO); lazy_static::initialize(&BUCKET);
create_bucket().await;
let mut pool: DbConnection = connection().expect("Failed to get db connection"); let mut pool: DbConnection = connection().expect("Failed to get db connection");
match pool.run_pending_migrations(MIGRATIONS) { match pool.run_pending_migrations(MIGRATIONS) {
Ok(_) => info!("Database initialized"), Ok(_) => info!("Database initialized"),
@@ -83,11 +85,34 @@ pub async fn redis_async_connection() -> Result<RedisConnection, ServiceError> {
Ok(conn) Ok(conn)
} }
pub async fn create_bucket(bucket_name: &str) -> Result<(), ServiceError> { async fn create_bucket() {
let exists = MINIO.bucket_exists(&BucketExistsArgs::new(&bucket_name).unwrap()).await?; let url = env::var("MINIO_URL").unwrap_or("localhost".to_string());
if !exists { let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
MINIO.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap()).await?; let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
} let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
let base_url = format!("http://{}:{}", url, port);
Ok(()) let region = Region::Custom {
region: "".to_string(),
endpoint: base_url,
};
let credentials = Credentials {
access_key: Some(user),
secret_key: Some(password),
security_token: None,
session_token: None,
expiration: None
};
let _ = Bucket::create_with_path_style("siren", region, credentials, BucketConfiguration::default()).await;
}
pub async fn upload_file(path: &str, content: &[u8]) -> Result<ResponseData, ServiceError> {
let response = BUCKET.put_object(path, content).await?;
Ok(response)
}
pub async fn delete_file(path: &str) -> Result<ResponseData, ServiceError> {
let response = BUCKET.delete_object(path).await?;
Ok(response)
} }

View File

@@ -54,10 +54,8 @@ export function refreshLoggedIn(interval = 840000) {
if (cookie != loggedIn) { if (cookie != loggedIn) {
loggedIn = cookie; loggedIn = cookie;
const response = await refresh(true); const response = await refresh(true);
if (response) { if (!response) {
Cookies.set('logged_in', 'true'); Cookies.remove('logged_in');
} else {
Cookies.set('test', 'failed');
} }
} }
}, interval); }, interval);