This commit is contained in:
2025-10-09 22:41:04 +02:00
parent 768f8cb21c
commit 4a428b9af6
13 changed files with 1019 additions and 50 deletions
+79
View File
@@ -0,0 +1,79 @@
use std::time::Duration;
use anyhow::{Context, Result};
use async_trait::async_trait;
use aws_sdk_s3::presigning::PresigningConfig;
use aws_sdk_s3::primitives::ByteStream;
use aws_sdk_s3::Client as S3Client;
#[async_trait]
pub trait ObjectStorage: Send + Sync + 'static {
async fn put_object(
&self,
key: &str,
bytes: Vec<u8>,
content_type: Option<String>,
) -> Result<()>;
async fn presign_get_object(&self, key: &str, expires_in: Duration) -> Result<String>;
}
pub struct S3Storage {
client: S3Client,
bucket: String,
}
impl S3Storage {
pub fn new(client: S3Client, bucket: impl Into<String>) -> Self {
Self {
client,
bucket: bucket.into(),
}
}
}
#[async_trait]
impl ObjectStorage for S3Storage {
async fn put_object(
&self,
key: &str,
bytes: Vec<u8>,
content_type: Option<String>,
) -> Result<()> {
let mut request = self
.client
.put_object()
.bucket(&self.bucket)
.key(key)
.body(ByteStream::from(bytes));
if let Some(content_type) = content_type {
request = request.content_type(content_type);
}
request
.send()
.await
.context("failed to upload object to S3")?;
Ok(())
}
async fn presign_get_object(&self, key: &str, expires_in: Duration) -> Result<String> {
let presign_config = PresigningConfig::builder()
.expires_in(expires_in)
.build()
.context("failed to build S3 presigning config")?;
let presigned = self
.client
.get_object()
.bucket(&self.bucket)
.key(key)
.presigned(presign_config)
.await
.context("failed to generate presigned download URL")?;
Ok(presigned.uri().to_string())
}
}