impl storage library

This commit is contained in:
2025-06-30 22:58:52 -04:00
parent a6b4f6917b
commit a64b8fdceb
6 changed files with 485 additions and 105 deletions

View File

@@ -29,6 +29,7 @@ pub struct TaskLog {
#[derive(Debug)]
pub enum TaskStatus {
Pending(String),
Running(String),
Completed(String),
Failed(String),
}

View File

@@ -1,7 +1,10 @@
use crate::utils::{
request::{Request, Response},
task_log,
{upload::S3ClientConfig, *},
};
use storage::services::{
aws::{S3Client, S3ClientConfig},
ObjectStorageClient,
};
pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) {
@@ -23,14 +26,14 @@ async fn upload_rss(pool: &sqlx::Pool<sqlx::Postgres>) -> Result<(), Box<dyn std
// upload the sucker to obj storage
if let Response::Xml(rss) = rss_result {
let client_config = S3ClientConfig::from_env().unwrap();
let s3_client = upload::create_s3_client(&client_config).await.unwrap();
let _ = upload::upload(
&s3_client,
client_config.bucket.as_str(),
"feed.xml",
rss.as_str(),
)
.await;
let s3_client = S3Client::new(&client_config);
let _ = s3_client
.put_object(
client_config.bucket.as_str(),
"feed.xml",
rss.as_bytes().to_vec(),
)
.await;
println!("Finished uploading RSS feed");
}

View File

@@ -1,7 +1,10 @@
use crate::utils::{
request::{Request, Response},
task_log,
{upload::S3ClientConfig, *},
};
use storage::services::{
aws::{S3Client, S3ClientConfig},
ObjectStorageClient,
};
pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) {
@@ -14,7 +17,7 @@ pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) {
async fn upload_sitemap(
pool: &sqlx::Pool<sqlx::Postgres>,
) -> Result<(), Box<dyn std::error::Error>> {
// TODO:: get sitemap and upload it to bucket??
// start task logging
task_log::start(3, pool).await?;
// get request and request the things
@@ -25,14 +28,14 @@ async fn upload_sitemap(
// upload the sucker to obj storage
if let Response::Xml(sitemap) = sitemap_result {
let client_config = S3ClientConfig::from_env().unwrap();
let s3_client = upload::create_s3_client(&client_config).await.unwrap();
let _ = upload::upload(
&s3_client,
client_config.bucket.as_str(),
"sitemap.xml",
sitemap.as_str(),
)
.await;
let s3_client = S3Client::new(&client_config);
let _ = s3_client
.put_object(
client_config.bucket.as_str(),
"sitemap.xml",
sitemap.as_bytes().to_vec(),
)
.await;
println!("Finished uploading sitemap!");
}

View File

@@ -1,73 +0,0 @@
use aws_config::{BehaviorVersion, Region};
use aws_sdk_s3::{config::Credentials, Client, Config};
use std::env;
#[derive(Debug)]
pub struct S3ClientConfig {
pub access_key: String,
secret_key: String,
endpoint: String,
pub bucket: String,
region: String,
}
impl S3ClientConfig {
pub fn from_env() -> Result<Self, Box<dyn std::error::Error>> {
Ok(S3ClientConfig {
access_key: env::var("LINODE_ACCESS_KEY")
.map_err(|_| "LINODE_ACCESS_KEY environment variable not set")?,
secret_key: env::var("LINODE_SECRET_KEY")
.map_err(|_| "LINODE_SECRET_KEY environment variable not set")?,
endpoint: env::var("LINODE_ENDPOINT")
.unwrap_or_else(|_| "us-ord-1.linodeobjects.com".to_string()),
bucket: env::var("LINODE_BUCKET")
.map_err(|_| "LINODE_BUCKET environment variable not set")?,
region: env::var("LINODE_REGION").unwrap_or_else(|_| "us-ord".to_string()),
})
}
}
pub async fn create_s3_client(
config: &S3ClientConfig,
) -> Result<Client, Box<dyn std::error::Error>> {
let credentials = Credentials::new(
&config.access_key,
&config.secret_key,
None,
None,
"linode-object-storage",
);
let s3_config = Config::builder()
.behavior_version(BehaviorVersion::latest())
.region(Region::new(config.region.clone()))
.endpoint_url(format!("https://{}", config.endpoint))
.credentials_provider(credentials)
.force_path_style(false)
.build();
Ok(Client::from_conf(s3_config))
}
pub async fn upload(
client: &Client,
bucket: &str,
key: &str,
content: &str,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Uploading to Linode Object Storage...");
println!("Bucket: {}", bucket);
let put_object_req = client
.put_object()
.bucket(bucket)
.key(key)
.body(content.as_bytes().to_vec().into())
.acl(aws_sdk_s3::types::ObjectCannedAcl::PublicRead)
.content_type("application/xml")
.send()
.await?;
println!("Upload successful! ETag: {:?}", put_object_req.e_tag());
Ok(())
}