2023-05-04 00:07:58 +00:00
|
|
|
use aws_sdk_s3::{
|
|
|
|
error::SdkError, operation::put_object::PutObjectError, primitives::ByteStream, Client,
|
|
|
|
};
|
2023-05-04 00:19:27 +00:00
|
|
|
use uuid::Uuid;
|
2023-05-04 00:07:58 +00:00
|
|
|
|
|
|
|
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,
|
2023-05-04 00:07:58 +00:00
|
|
|
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}");
|
2023-05-04 00:07:58 +00:00
|
|
|
match client
|
|
|
|
.put_object()
|
2023-05-04 00:19:27 +00:00
|
|
|
.bucket("realtor-lp-bucket")
|
2023-05-04 00:07:58 +00:00
|
|
|
.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}"
|
2023-05-04 00:07:58 +00:00
|
|
|
)),
|
|
|
|
Err(error) => Err(error),
|
|
|
|
}
|
|
|
|
}
|