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

34 lines
938 B
Rust
Raw Normal View History

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