| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use super::bucket::S3DeleteAuthority; |
| 5 |
use super::key::S3Key; |
| 6 |
use crate::error::{AppError, Result}; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
pub(crate) async fn read_bytestream_capped( |
| 13 |
mut stream: s3_storage::ByteStream, |
| 14 |
label: &str, |
| 15 |
max_bytes: u64, |
| 16 |
) -> Result<bytes::Bytes> { |
| 17 |
let mut buf = bytes::BytesMut::new(); |
| 18 |
let mut read: u64 = 0; |
| 19 |
loop { |
| 20 |
match stream.try_next().await { |
| 21 |
Ok(Some(chunk)) => { |
| 22 |
read += chunk.len() as u64; |
| 23 |
if read > max_bytes { |
| 24 |
return Err(AppError::Storage(format!( |
| 25 |
"object {label} exceeds scan in-memory cap ({read} > {max_bytes} bytes); \ |
| 26 |
recorded size under-reported the real object" |
| 27 |
))); |
| 28 |
} |
| 29 |
buf.extend_from_slice(&chunk); |
| 30 |
} |
| 31 |
Ok(None) => break, |
| 32 |
Err(e) => return Err(AppError::Storage(format!("read object from S3: {e}"))), |
| 33 |
} |
| 34 |
} |
| 35 |
Ok(buf.freeze()) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#[async_trait::async_trait] |
| 41 |
pub trait StorageBackend: Send + Sync { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
async fn presign_upload( |
| 47 |
&self, |
| 48 |
s3_key: &S3Key, |
| 49 |
content_type: &str, |
| 50 |
expiry_secs: Option<u64>, |
| 51 |
cache_control: Option<&str>, |
| 52 |
max_bytes: Option<i64>, |
| 53 |
) -> Result<String>; |
| 54 |
async fn presign_download(&self, s3_key: &S3Key, expiry_secs: Option<u64>) -> Result<String>; |
| 55 |
async fn object_exists(&self, s3_key: &str) -> Result<bool>; |
| 56 |
async fn object_size(&self, s3_key: &str) -> Result<Option<i64>>; |
| 57 |
async fn download_object(&self, s3_key: &str) -> Result<Vec<u8>>; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
async fn download_object_buf(&self, s3_key: &str) -> Result<bytes::Bytes>; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
async fn download_stream(&self, s3_key: &str) -> Result<s3_storage::ByteStream>; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
async fn download_object_buf_capped( |
| 74 |
&self, |
| 75 |
s3_key: &str, |
| 76 |
max_bytes: u64, |
| 77 |
) -> Result<bytes::Bytes> { |
| 78 |
let stream = self.download_stream(s3_key).await?; |
| 79 |
read_bytestream_capped(stream, s3_key, max_bytes).await |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
async fn download_head(&self, s3_key: &str, len: usize) -> Result<Vec<u8>> { |
| 86 |
let mut stream = self.download_stream(s3_key).await?; |
| 87 |
let mut head = Vec::with_capacity(len.min(64 * 1024)); |
| 88 |
while head.len() < len { |
| 89 |
match stream.try_next().await { |
| 90 |
Ok(Some(chunk)) => head.extend_from_slice(&chunk), |
| 91 |
Ok(None) => break, |
| 92 |
Err(e) => return Err(AppError::Storage(format!("read object head from S3: {e}"))), |
| 93 |
} |
| 94 |
} |
| 95 |
head.truncate(len); |
| 96 |
Ok(head) |
| 97 |
} |
| 98 |
async fn upload_object( |
| 99 |
&self, |
| 100 |
s3_key: &S3Key, |
| 101 |
content_type: &str, |
| 102 |
data: Vec<u8>, |
| 103 |
cache_control: Option<&str>, |
| 104 |
) -> Result<()>; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
async fn delete_object(&self, auth: &S3DeleteAuthority, s3_key: &S3Key) -> Result<()>; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
async fn delete_objects(&self, auth: &S3DeleteAuthority, keys: &[S3Key]) -> Result<()> { |
| 113 |
let mut failed = 0usize; |
| 114 |
for k in keys { |
| 115 |
if let Err(e) = self.delete_object(auth, k).await { |
| 116 |
failed += 1; |
| 117 |
tracing::warn!(key = %k, error = ?e, "delete_objects: per-key delete failed"); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if !keys.is_empty() && failed == keys.len() { |
| 124 |
return Err(AppError::Storage(format!( |
| 125 |
"delete_objects: all {failed} keys failed" |
| 126 |
))); |
| 127 |
} |
| 128 |
Ok(()) |
| 129 |
} |
| 130 |
|
| 131 |
async fn delete_prefix(&self, _auth: &S3DeleteAuthority, _prefix: &str) -> Result<()> { |
| 132 |
tracing::warn!("delete_prefix called on a storage backend that does not implement it"); |
| 133 |
Ok(()) |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
async fn upload_multipart( |
| 140 |
&self, |
| 141 |
s3_key: &S3Key, |
| 142 |
content_type: &str, |
| 143 |
file_path: &std::path::Path, |
| 144 |
) -> Result<()>; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
async fn copy_object(&self, src_key: &S3Key, dst_key: &S3Key) -> Result<()>; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
async fn copy_object_from( |
| 158 |
&self, |
| 159 |
src_bucket: &str, |
| 160 |
src_key: &S3Key, |
| 161 |
dst_key: &S3Key, |
| 162 |
) -> Result<()>; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
async fn copy_object_multipart( |
| 172 |
&self, |
| 173 |
src_bucket: &str, |
| 174 |
src_key: &S3Key, |
| 175 |
dst_key: &S3Key, |
| 176 |
content_type: &str, |
| 177 |
src_size: u64, |
| 178 |
part_size: Option<usize>, |
| 179 |
) -> Result<()>; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
async fn create_multipart_upload(&self, s3_key: &S3Key, content_type: &str) -> Result<String>; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
async fn presign_upload_part( |
| 205 |
&self, |
| 206 |
s3_key: &S3Key, |
| 207 |
upload_id: &str, |
| 208 |
part_number: i32, |
| 209 |
expiry_secs: Option<u64>, |
| 210 |
max_bytes: Option<i64>, |
| 211 |
checksum_sha256: Option<&str>, |
| 212 |
) -> Result<String>; |
| 213 |
|
| 214 |
|
| 215 |
async fn complete_multipart_upload( |
| 216 |
&self, |
| 217 |
s3_key: &S3Key, |
| 218 |
upload_id: &str, |
| 219 |
parts: &[(i32, String)], |
| 220 |
) -> Result<()>; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
async fn abort_multipart_upload(&self, s3_key: &S3Key, upload_id: &str) -> Result<()>; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
async fn list_multipart_uploads_for_key(&self, s3_key: &str) -> Result<Vec<String>>; |
| 230 |
async fn check_connectivity(&self) -> std::result::Result<(), String>; |
| 231 |
fn bucket(&self) -> &str; |
| 232 |
} |
| 233 |
|