use std::time::Duration;
use crate::{Error, Result};
use aws_sdk_s3::error::SdkError;
use aws_sdk_s3::presigning::PresigningConfig;
use aws_sdk_s3::Client as S3Client;
use reqwest::Client as HttpClient;
use super::DEAFULT_EXPIRATION;
pub async fn presign_delete(
s3: &S3Client,
bucket: impl AsRef<str>,
key: impl AsRef<str>,
expiration: Option<Duration>,
) -> Result<String> {
let presigning_config = PresigningConfig::builder()
.expires_in(expiration.unwrap_or(DEAFULT_EXPIRATION))
.build()
.map_err(Error::PresigningConfigError)?;
let presigned_req = s3
.delete_object()
.bucket(bucket.as_ref().to_string())
.key(key.as_ref().to_string())
.presigned(presigning_config)
.await
.map_err(|e| match e {
SdkError::ServiceError(error) => Error::DeleteObjectFailed(Box::new(error.into_err())),
_ => Error::SdkError(e.to_string()),
})?;
Ok(presigned_req.uri().to_string())
}
pub async fn delete_file_presigned(
client: &HttpClient,
presigned_url: impl AsRef<str>,
) -> Result<()> {
client
.delete(presigned_url.as_ref())
.send()
.await?
.error_for_status()?;
Ok(())
}