realtor-lp-backend/src/utils/s3.rs
2023-05-03 20:19:27 -04:00

34 lines
938 B
Rust

use aws_sdk_s3::{
error::SdkError, operation::put_object::PutObjectError, primitives::ByteStream, Client,
};
use uuid::Uuid;
pub async fn init_aws_client() -> Client {
dotenvy::dotenv().expect("Failed loading env");
let config = aws_config::load_from_env().await;
Client::new(&config)
}
pub async fn upload_image(
client: &Client,
realtor_id: Uuid,
file_name: String,
multipart: Vec<u8>,
) -> Result<String, SdkError<PutObjectError>> {
let path = format!("realtor-images/{realtor_id}/{file_name}");
match client
.put_object()
.bucket("realtor-lp-bucket")
.key(path.clone())
.body(ByteStream::from(multipart))
.acl(aws_sdk_s3::types::ObjectCannedAcl::PublicRead)
.send()
.await
{
Ok(_) => Ok(format!(
"https://realtor-lp-bucket.s3.us-east-2.amazonaws.com/{path}"
)),
Err(error) => Err(error),
}
}