use async_trait::async_trait;
use crate::{
StorageResult, StorageFile, StorageConfig, StorageStats,
UploadOptions, DownloadOptions, PresignedUrlOptions, ByteRange,
types::EncryptionConfig,
};
use std::collections::HashMap;
use bytes::Bytes;
#[async_trait]
pub trait StorageService: Send + Sync {
async fn upload_bytes(
&self,
path: &str,
data: Bytes,
options: Option<UploadOptions>,
) -> StorageResult<StorageFile>;
async fn upload_reader(
&self,
path: &str,
reader: Box<dyn tokio::io::AsyncRead + Send + Unpin>,
size: Option<u64>,
options: Option<UploadOptions>,
) -> StorageResult<StorageFile>;
async fn upload_file(
&self,
path: &str,
local_path: &str,
options: Option<UploadOptions>,
) -> StorageResult<StorageFile>;
async fn download_bytes(
&self,
path: &str,
options: Option<DownloadOptions>,
) -> StorageResult<Bytes>;
async fn download_writer(
&self,
path: &str,
writer: Box<dyn tokio::io::AsyncWrite + Send + Unpin>,
options: Option<DownloadOptions>,
) -> StorageResult<()>;
async fn download_file(
&self,
path: &str,
local_path: &str,
options: Option<DownloadOptions>,
) -> StorageResult<()>;
async fn stream_file(
&self,
path: &str,
range: Option<ByteRange>,
) -> StorageResult<Box<dyn futures::stream::Stream<Item = StorageResult<Bytes>> + Send + Unpin>>;
async fn get_file(&self, path: &str) -> StorageResult<StorageFile>;
async fn file_exists(&self, path: &str) -> StorageResult<bool>;
async fn delete_file(&self, path: &str) -> StorageResult<bool>;
async fn copy_file(&self, from_path: &str, to_path: &str) -> StorageResult<StorageFile>;
async fn move_file(&self, from_path: &str, to_path: &str) -> StorageResult<StorageFile>;
async fn list_files(
&self,
prefix: &str,
limit: Option<u32>,
continuation_token: Option<String>,
) -> StorageResult<FileListResult>;
async fn generate_presigned_url(
&self,
path: &str,
options: PresignedUrlOptions,
) -> StorageResult<String>;
async fn get_file_url(
&self,
path: &str,
expiry_seconds: Option<u64>,
) -> StorageResult<String>;
async fn update_metadata(
&self,
path: &str,
metadata: HashMap<String, String>,
) -> StorageResult<StorageFile>;
async fn get_stats(&self) -> StorageResult<StorageStats>;
async fn test_connection(&self) -> StorageResult<bool>;
fn backend_type(&self) -> crate::StorageBackend;
fn bucket(&self) -> &str;
fn config(&self) -> &StorageConfig;
}
#[async_trait]
pub trait StorageManager: Send + Sync {
async fn create_bucket(&self, bucket_name: &str, config: Option<BucketConfig>) -> StorageResult<bool>;
async fn delete_bucket(&self, bucket_name: &str, force: bool) -> StorageResult<bool>;
async fn list_buckets(&self) -> StorageResult<Vec<String>>;
async fn get_bucket_info(&self, bucket_name: &str) -> StorageResult<BucketInfo>;
async fn set_lifecycle_rules(
&self,
bucket_name: &str,
rules: Vec<LifecycleRule>,
) -> StorageResult<bool>;
async fn enable_versioning(&self, bucket_name: &str) -> StorageResult<bool>;
async fn disable_versioning(&self, bucket_name: &str) -> StorageResult<bool>;
async fn set_access_control(
&self,
bucket_name: &str,
policy: AccessPolicy,
) -> StorageResult<bool>;
async fn get_access_control(&self, bucket_name: &str) -> StorageResult<AccessPolicy>;
async fn restore_backup(
&self,
backup_path: &str,
target_bucket: &str,
target_prefix: Option<String>,
) -> StorageResult<RestoreResult>;
async fn create_backup(
&self,
bucket_name: &str,
backup_path: &str,
prefix_filter: Option<String>,
) -> StorageResult<BackupResult>;
}
#[async_trait]
pub trait StorageMonitor: Send + Sync {
async fn start_monitoring(&self, bucket_name: &str) -> StorageResult<()>;
async fn stop_monitoring(&self, bucket_name: &str) -> StorageResult<()>;
async fn get_metrics(&self, bucket_name: &str) -> StorageResult<HashMap<String, f64>>;
async fn set_alert_thresholds(
&self,
bucket_name: &str,
thresholds: AlertThresholds,
) -> StorageResult<()>;
async fn get_alerts(&self, bucket_name: &str) -> StorageResult<Vec<StorageAlert>>;
async fn get_usage_trends(
&self,
bucket_name: &str,
period: TrendPeriod,
) -> StorageResult<UsageTrend>;
async fn generate_report(
&self,
bucket_name: &str,
report_type: ReportType,
) -> StorageResult<StorageReport>;
}
#[derive(Debug, Clone)]
pub struct FileListResult {
pub files: Vec<StorageFile>,
pub prefixes: Vec<String>,
pub truncated: bool,
pub next_continuation_token: Option<String>,
pub total_count: Option<u64>,
}
#[derive(Debug, Clone)]
pub struct BucketConfig {
pub region: Option<String>,
pub access_policy: Option<AccessPolicy>,
pub storage_class: Option<String>,
pub versioning: Option<bool>,
pub lifecycle_rules: Option<Vec<LifecycleRule>>,
pub tags: Option<HashMap<String, String>>,
pub encryption: Option<EncryptionConfig>,
}
#[derive(Debug, Clone)]
pub struct BucketInfo {
pub name: String,
pub created_at: chrono::DateTime<chrono::Utc>,
pub region: String,
pub storage_class: String,
pub versioning: VersioningStatus,
pub access_policy: AccessPolicy,
pub size_bytes: u64,
pub object_count: u64,
pub tags: HashMap<String, String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VersioningStatus {
Enabled,
Suspended,
Disabled,
}
#[derive(Debug, Clone)]
pub struct LifecycleRule {
pub id: String,
pub status: LifecycleRuleStatus,
pub filter: LifecycleFilter,
pub action: LifecycleAction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LifecycleRuleStatus {
Enabled,
Disabled,
}
#[derive(Debug, Clone)]
pub struct LifecycleFilter {
pub prefix: Option<String>,
pub tags: Option<HashMap<String, String>>,
pub size_range: Option<(u64, u64)>,
}
#[derive(Debug, Clone)]
pub struct LifecycleAction {
pub action_type: LifecycleActionType,
pub days: Option<u32>,
pub storage_class: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LifecycleActionType {
Delete,
Transition,
Expire,
Archive,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessPolicy {
Private,
PublicRead,
PublicReadWrite,
Custom,
}
#[derive(Debug, Clone)]
pub struct RestoreResult {
pub files_restored: u64,
pub bytes_restored: u64,
pub files_failed: u64,
pub errors: Vec<String>,
pub duration_seconds: u64,
}
#[derive(Debug, Clone)]
pub struct BackupResult {
pub files_backed_up: u64,
pub bytes_backed_up: u64,
pub backup_path: String,
pub backup_checksum: String,
pub files_skipped: u64,
pub errors: Vec<String>,
pub duration_seconds: u64,
}
#[derive(Debug, Clone)]
pub struct AlertThresholds {
pub storage_usage_percent: Option<f64>,
pub file_count_limit: Option<u64>,
pub large_file_threshold: Option<u64>,
pub error_rate_percent: Option<f64>,
pub avg_response_time_ms: Option<f64>,
}
#[derive(Debug, Clone)]
pub struct StorageAlert {
pub id: String,
pub alert_type: StorageAlertType,
pub severity: AlertSeverity,
pub message: String,
pub timestamp: chrono::DateTime<chrono::Utc>,
pub bucket: String,
pub details: HashMap<String, String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StorageAlertType {
StorageUsage,
FileCount,
LargeFile,
ErrorRate,
ResponseTime,
ConnectionFailure,
PermissionDenied,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlertSeverity {
Info,
Warning,
Error,
Critical,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrendPeriod {
LastHour,
Last24Hours,
Last7Days,
Last30Days,
Custom { hours: u32 },
}
#[derive(Debug, Clone)]
pub struct UsageTrend {
pub period: TrendPeriod,
pub data_points: Vec<TrendDataPoint>,
pub growth_rate: f64,
pub predicted_usage: Option<u64>,
}
#[derive(Debug, Clone)]
pub struct TrendDataPoint {
pub timestamp: chrono::DateTime<chrono::Utc>,
pub usage_bytes: u64,
pub file_count: u64,
pub operations: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReportType {
Usage,
Performance,
Cost,
Security,
Compliance,
}
#[derive(Debug, Clone)]
pub struct StorageReport {
pub report_type: ReportType,
pub generated_at: chrono::DateTime<chrono::Utc>,
pub data: serde_json::Value,
pub summary: HashMap<String, String>,
pub recommendations: Vec<String>,
}