|
| 1 | +use aligned_sdk::core::types::VerificationData; |
| 2 | +use aws_config::{meta::region::RegionProviderChain, BehaviorVersion}; |
| 3 | +use aws_sdk_s3::Client; |
| 4 | +use tracing::info; |
| 5 | + |
| 6 | +pub struct S3Client { |
| 7 | + client: Client, |
| 8 | + bucket_name: String, |
| 9 | +} |
| 10 | + |
| 11 | +pub enum GetBatchProofsError { |
| 12 | + Fetching, |
| 13 | + Deserialization, |
| 14 | + EmptyBody, |
| 15 | +} |
| 16 | + |
| 17 | +impl S3Client { |
| 18 | + pub async fn new(bucket_name: String, endpoint_url: Option<String>) -> Self { |
| 19 | + let region_provider = RegionProviderChain::default_provider().or_else("us-east-2"); |
| 20 | + let mut config = aws_config::defaults(BehaviorVersion::latest()).region(region_provider); |
| 21 | + if let Some(endpoint_url) = &endpoint_url { |
| 22 | + info!("Using custom endpoint: {}", endpoint_url); |
| 23 | + config = config.endpoint_url(endpoint_url); |
| 24 | + } |
| 25 | + let config = config.load().await; |
| 26 | + |
| 27 | + let mut s3_config_builder = aws_sdk_s3::config::Builder::from(&config); |
| 28 | + if endpoint_url.is_some() { |
| 29 | + info!("Forcing path style for custom endpoint"); |
| 30 | + s3_config_builder = s3_config_builder.force_path_style(true); |
| 31 | + } |
| 32 | + |
| 33 | + let client = Client::from_conf(s3_config_builder.build()); |
| 34 | + |
| 35 | + Self { |
| 36 | + client, |
| 37 | + bucket_name, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub async fn get_aligned_batch( |
| 42 | + &self, |
| 43 | + key: String, |
| 44 | + ) -> Result<Vec<VerificationData>, GetBatchProofsError> { |
| 45 | + let result = self |
| 46 | + .client |
| 47 | + .get_object() |
| 48 | + .bucket(self.bucket_name.clone()) |
| 49 | + .key(key) |
| 50 | + .send() |
| 51 | + .await |
| 52 | + .map_err(|_| GetBatchProofsError::Fetching)?; |
| 53 | + |
| 54 | + let Some(bytes) = result.body.bytes() else { |
| 55 | + return Err(GetBatchProofsError::EmptyBody); |
| 56 | + }; |
| 57 | + |
| 58 | + let data: Vec<VerificationData> = |
| 59 | + ciborium::from_reader(bytes).map_err(|_| GetBatchProofsError::Deserialization)?; |
| 60 | + |
| 61 | + Ok(data) |
| 62 | + } |
| 63 | +} |
0 commit comments