realtor-lp-backend/src/utils/s3.rs

32 lines
888 B
Rust
Raw Normal View History

use aws_sdk_s3::{
error::SdkError, operation::put_object::PutObjectError, primitives::ByteStream, Client,
};
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,
file_name: String,
multipart: Vec<u8>,
) -> Result<String, SdkError<PutObjectError>> {
let path = format!("jl-images/{file_name}");
match client
.put_object()
.bucket("jorge-ledesma-bucket")
.key(path.clone())
.body(ByteStream::from(multipart))
.acl(aws_sdk_s3::types::ObjectCannedAcl::PublicRead)
.send()
.await
{
Ok(_) => Ok(format!(
"https://jorge-ledesma-bucket.s3.us-east-2.amazonaws.com/{path}"
)),
Err(error) => Err(error),
}
}