Use AWS SDK (#5959)

This commit is contained in:
François-Xavier Talbot
2026-05-01 05:58:26 -04:00
committed by GitHub
parent 4ddb5640cf
commit 264aade726
5 changed files with 491 additions and 78 deletions

View File

@@ -29,6 +29,7 @@ async-stripe = { workspace = true, features = [
"webhook-events"
] }
async-trait = { workspace = true }
aws-sdk-s3 = { workspace = true }
base64 = { workspace = true }
bitflags = { workspace = true }
bytes = { workspace = true }
@@ -96,7 +97,6 @@ rust_decimal = { workspace = true, features = [
"serde-with-str"
] }
rust_iso3166 = { workspace = true }
rust-s3 = { workspace = true }
rustls.workspace = true
rusty-money = { workspace = true }
sentry = { workspace = true }

View File

@@ -1,3 +1,4 @@
use std::error::Error;
use std::str::FromStr;
use async_trait::async_trait;
@@ -13,7 +14,7 @@ pub use s3_host::{S3BucketConfig, S3Host};
#[derive(Error, Debug)]
pub enum FileHostingError {
#[error("S3 error when {0}: {1}")]
S3Error(&'static str, s3::error::S3Error),
S3Error(&'static str, #[source] Box<dyn Error + Send + Sync>),
#[error("File system error in file hosting: {0}")]
FileSystemError(#[from] std::io::Error),
#[error("Invalid Filename")]

View File

@@ -3,13 +3,16 @@ use crate::file_hosting::{
UploadFileData,
};
use async_trait::async_trait;
use aws_sdk_s3::Client;
use aws_sdk_s3::config::{BehaviorVersion, Credentials, Region};
use aws_sdk_s3::presigning::PresigningConfig;
use aws_sdk_s3::primitives::ByteStream;
use bytes::Bytes;
use chrono::Utc;
use hex::ToHex;
use s3::bucket::Bucket;
use s3::creds::Credentials;
use s3::region::Region;
use sha2::Digest;
use std::error::Error;
use std::time::Duration;
pub struct S3BucketConfig {
pub name: String,
@@ -21,8 +24,13 @@ pub struct S3BucketConfig {
}
pub struct S3Host {
public_bucket: Bucket,
private_bucket: Bucket,
public_bucket: S3Bucket,
private_bucket: S3Bucket,
}
struct S3Bucket {
name: String,
client: Client,
}
impl S3Host {
@@ -30,47 +38,45 @@ impl S3Host {
public_bucket: S3BucketConfig,
private_bucket: S3BucketConfig,
) -> Result<S3Host, FileHostingError> {
let create_bucket =
|config: S3BucketConfig| -> Result<_, FileHostingError> {
let mut bucket = Bucket::new(
"",
if config.region == "r2" {
Region::R2 {
account_id: config.url,
}
} else {
Region::Custom {
region: config.region,
endpoint: config.url,
}
},
Credentials {
access_key: Some(config.access_token),
secret_key: Some(config.secret),
..Credentials::anonymous().unwrap()
},
let create_bucket = |config: S3BucketConfig| -> S3Bucket {
let (region, endpoint_url, provider_name) = if config.region == "r2"
{
(
"auto".to_string(),
format!("https://{}.r2.cloudflarestorage.com", config.url),
"R2",
)
.map_err(|e| {
FileHostingError::S3Error("creating Bucket instance", e)
})?;
bucket.name = config.name;
if config.uses_path_style {
bucket.set_path_style();
} else {
bucket.set_subdomain_style();
}
Ok(bucket)
} else {
(config.region, config.url, "Labrinth")
};
let s3_config = aws_sdk_s3::config::Builder::new()
.behavior_version(BehaviorVersion::latest())
.region(Region::new(region))
.endpoint_url(endpoint_url)
.credentials_provider(Credentials::new(
config.access_token,
config.secret,
None,
None,
provider_name,
))
.force_path_style(config.uses_path_style)
.build();
S3Bucket {
name: config.name,
client: Client::from_conf(s3_config),
}
};
Ok(S3Host {
public_bucket: *create_bucket(public_bucket)?,
private_bucket: *create_bucket(private_bucket)?,
public_bucket: create_bucket(public_bucket),
private_bucket: create_bucket(private_bucket),
})
}
fn get_bucket(&self, publicity: FileHostPublicity) -> &Bucket {
fn get_bucket(&self, publicity: FileHostPublicity) -> &S3Bucket {
match publicity {
FileHostPublicity::Public => &self.public_bucket,
FileHostPublicity::Private => &self.private_bucket,
@@ -78,6 +84,13 @@ impl S3Host {
}
}
fn s3_error(
context: &'static str,
error: impl Error + Send + Sync + 'static,
) -> FileHostingError {
FileHostingError::S3Error(context, Box::new(error))
}
#[async_trait]
impl FileHost for S3Host {
async fn upload_file(
@@ -89,20 +102,24 @@ impl FileHost for S3Host {
) -> Result<UploadFileData, FileHostingError> {
let content_sha1 = sha1::Sha1::digest(&file_bytes).encode_hex();
let content_sha512 = format!("{:x}", sha2::Sha512::digest(&file_bytes));
let content_length = file_bytes.len() as u32;
let bucket = self.get_bucket(file_publicity);
self.get_bucket(file_publicity)
.put_object_with_content_type(
format!("/{file_name}"),
&file_bytes,
content_type,
)
bucket
.client
.put_object()
.bucket(bucket.name.as_str())
.key(file_name)
.content_type(content_type)
.body(ByteStream::from(file_bytes))
.send()
.await
.map_err(|e| FileHostingError::S3Error("uploading file", e))?;
.map_err(|e| s3_error("uploading file", e))?;
Ok(UploadFileData {
file_name: file_name.to_string(),
file_publicity,
content_length: file_bytes.len() as u32,
content_length,
content_sha512,
content_sha1,
content_md5: None,
@@ -116,14 +133,20 @@ impl FileHost for S3Host {
file_name: &str,
expiry_secs: u32,
) -> Result<String, FileHostingError> {
let presigning_config = PresigningConfig::expires_in(
Duration::from_secs(expiry_secs.into()),
)
.map_err(|e| s3_error("creating presigning config", e))?;
let url = self
.private_bucket
.presign_get(format!("/{file_name}"), expiry_secs, None)
.client
.get_object()
.bucket(self.private_bucket.name.as_str())
.key(file_name)
.presigned(presigning_config)
.await
.map_err(|e| {
FileHostingError::S3Error("generating presigned URL", e)
})?;
Ok(url)
.map_err(|e| s3_error("generating presigned URL", e))?;
Ok(url.uri().to_string())
}
async fn delete_file(
@@ -131,10 +154,16 @@ impl FileHost for S3Host {
file_name: &str,
file_publicity: FileHostPublicity,
) -> Result<DeleteFileData, FileHostingError> {
self.get_bucket(file_publicity)
.delete_object(format!("/{file_name}"))
let bucket = self.get_bucket(file_publicity);
bucket
.client
.delete_object()
.bucket(bucket.name.as_str())
.key(file_name)
.send()
.await
.map_err(|e| FileHostingError::S3Error("deleting file", e))?;
.map_err(|e| s3_error("deleting file", e))?;
Ok(DeleteFileData {
file_name: file_name.to_string(),