s3cli 0.1.1

CLI-first S3 storage for developers and AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use async_trait::async_trait;
use std::io::Read;
use std::time::{SystemTime, UNIX_EPOCH};
use hmac::{Hmac, Mac};
use sha2::Sha256;
use reqwest::{Client, header::{HeaderMap, HeaderValue, CONTENT_TYPE, CONTENT_LENGTH}};
use crate::config::{Config, Provider};
use crate::models::{FileEntry, FileMetadata, Pagination};
use crate::storage::{StorageError, StreamedData, ListResult};

type HmacSha256 = Hmac<Sha256>;

pub struct S3Storage {
    client: Client,
    config: Config,
    bucket: String,
    endpoint: String,
}

impl S3Storage {
    pub async fn new(config: &Config) -> Result<Self, StorageError> {
        let client = Client::builder()
            .build()
            .map_err(|e| StorageError::IoError(e.to_string()))?;
        
        let endpoint = config.endpoint.clone()
            .unwrap_or_else(|| format!("https://{}.s3.{}.amazonaws.com", config.bucket, config.region));
        
        Ok(Self {
            client,
            config: config.clone(),
            bucket: config.bucket.clone(),
            endpoint,
        })
    }
    
    fn generate_id() -> String {
        use uuid::Uuid;
        let uuid = Uuid::new_v4();
        let bytes = uuid.as_bytes();
        crockford_encode(bytes)
    }
    
    fn sign_request(
        &self,
        method: &str,
        path: &str,
        query: &str,
        headers: &mut HeaderMap,
        payload_hash: &str,
    ) -> Result<(), StorageError> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| StorageError::IoError(e.to_string()))?;
        
        let date = chrono::Utc::now();
        let amz_date = date.format("%Y%m%dT%H%M%SZ").to_string();
        let date_stamp = date.format("%Y%m%d").to_string();
        
        headers.insert("x-amz-date", HeaderValue::from_str(&amz_date).unwrap());
        headers.insert("x-amz-content-sha256", HeaderValue::from_str(payload_hash).unwrap());
        
        let host = self.endpoint.replace("https://", "").replace("http://", "");
        headers.insert("host", HeaderValue::from_str(&host).unwrap());
        
        let canonical_headers = format!(
            "host:{}\nx-amz-content-sha256:{}\nx-amz-date:{}\n",
            host, payload_hash, amz_date
        );
        let signed_headers = "host;x-amz-content-sha256;x-amz-date";
        
        let canonical_request = format!(
            "{}\n{}\n{}\n{}\n{}\n{}",
            method,
            path,
            query,
            canonical_headers,
            signed_headers,
            payload_hash
        );
        
        let canonical_request_hash = sha256_hex(canonical_request.as_bytes());
        
        let credential_scope = format!("{}/{}/s3/aws4_request", date_stamp, self.config.region);
        let string_to_sign = format!(
            "AWS4-HMAC-SHA256\n{}\n{}\n{}",
            amz_date, credential_scope, canonical_request_hash
        );
        
        let k_date = hmac_sha256(format!("AWS4{}", self.config.secret_key.as_ref().unwrap()).as_bytes(), date_stamp.as_bytes());
        let k_region = hmac_sha256(&k_date, self.config.region.as_bytes());
        let k_service = hmac_sha256(&k_region, b"s3");
        let k_signing = hmac_sha256(&k_service, b"aws4_request");
        let signature = hmac_sha256_hex(&k_signing, string_to_sign.as_bytes());
        
        let auth_header = format!(
            "AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
            self.config.access_key.as_ref().unwrap(),
            credential_scope,
            signed_headers,
            signature
        );
        
        headers.insert("Authorization", HeaderValue::from_str(&auth_header).unwrap());
        
        Ok(())
    }
    
    async fn request(
        &self,
        method: &str,
        path: &str,
        query: &str,
        body: Option<Vec<u8>>,
        content_type: Option<&str>,
    ) -> Result<reqwest::Response, StorageError> {
        let mut headers = HeaderMap::new();
        
        let payload_hash = if let Some(ref b) = body {
            sha256_hex(b)
        } else {
            sha256_hex(b"")
        };
        
        if let Some(ct) = content_type {
            headers.insert(CONTENT_TYPE, HeaderValue::from_str(ct).unwrap());
        }
        
        if let Some(ref b) = body {
            headers.insert(CONTENT_LENGTH, HeaderValue::from_str(&b.len().to_string()).unwrap());
        }
        
        self.sign_request(method, path, query, &mut headers, &payload_hash)?;
        
        let url = format!("{}{}?{}", self.endpoint, path, query);
        
        let mut request = self.client.request(
            reqwest::Method::from_bytes(method.as_bytes()).unwrap(),
            &url
        ).headers(headers);
        
        if let Some(b) = body {
            request = request.body(b);
        }
        
        request.send().await.map_err(|e| StorageError::NetworkError(e.to_string()))
    }
}

fn crockford_encode(bytes: &[u8]) -> String {
    const ALPHABET: &[u8] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
    let mut result = String::with_capacity(bytes.len() * 8 / 5 + 1);
    
    let mut buffer: u64 = 0;
    let mut bits_in_buffer = 0;
    
    for &byte in bytes {
        buffer = (buffer << 8) | (byte as u64);
        bits_in_buffer += 8;
        
        while bits_in_buffer >= 5 {
            bits_in_buffer -= 5;
            let index = ((buffer >> bits_in_buffer) & 0x1F) as usize;
            result.push(ALPHABET[index] as char);
        }
    }
    
    if bits_in_buffer > 0 {
        let index = ((buffer << (5 - bits_in_buffer)) & 0x1F) as usize;
        result.push(ALPHABET[index] as char);
    }
    
    result
}

fn sha256_hex(data: &[u8]) -> String {
    use sha2::{Sha256, Digest};
    let mut hasher = Sha256::new();
    hasher.update(data);
    hex::encode(hasher.finalize())
}

fn hmac_sha256(key: &[u8], data: &[u8]) -> Vec<u8> {
    let mut mac = HmacSha256::new_from_slice(key).expect("HMAC can take key of any size");
    mac.update(data);
    mac.finalize().into_bytes().to_vec()
}

fn hmac_sha256_hex(key: &[u8], data: &[u8]) -> String {
    hex::encode(hmac_sha256(key, data))
}

#[async_trait]
impl crate::storage::Storage for S3Storage {
    async fn put(
        &self, 
        key: &str, 
        data: Box<dyn Read + Send + Sync>, 
        metadata: &FileMetadata
    ) -> Result<FileEntry, StorageError> {
        let mut buffer = Vec::new();
        let mut reader = data;
        reader.read_to_end(&mut buffer).map_err(|e| StorageError::IoError(e.to_string()))?;
        
        let content_type = metadata.content_type.clone()
            .unwrap_or_else(|| "application/octet-stream".to_string());
        
        let path = format!("/{}", key);
        
        let response = self.request(
            "PUT",
            &path,
            "",
            Some(buffer.clone()),
            Some(&content_type),
        ).await?;
        
        if !response.status().is_success() {
            return Err(StorageError::ProviderError(format!(
                "Upload failed: {}",
                response.status()
            )));
        }
        
        let size = buffer.len() as u64;
        
        Ok(FileEntry::new(
            Self::generate_id(),
            key.to_string(),
            size,
            content_type,
        ))
    }
    
    async fn get(&self, key: &str) -> Result<StreamedData, StorageError> {
        let path = format!("/{}", key);
        
        let response = self.request("GET", &path, "", None, None).await?;
        
        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(StorageError::NotFound(key.to_string()));
        }
        
        if !response.status().is_success() {
            return Err(StorageError::ProviderError(format!(
                "Download failed: {}",
                response.status()
            )));
        }
        
        let content_type = response.headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("application/octet-stream")
            .to_string();
        
        let content_length = response.headers()
            .get("content-length")
            .and_then(|v| v.to_str().ok())
            .and_then(|v| v.parse().ok())
            .unwrap_or(0);
        
        let data = response.bytes().await.map_err(|e| StorageError::NetworkError(e.to_string()))?;
        
        Ok(StreamedData {
            data,
            content_type,
            content_length,
        })
    }
    
    async fn delete(&self, key: &str) -> Result<(), StorageError> {
        let path = format!("/{}", key);
        
        let response = self.request("DELETE", &path, "", None, None).await?;
        
        if !response.status().is_success() && response.status() != reqwest::StatusCode::NOT_FOUND {
            return Err(StorageError::ProviderError(format!(
                "Delete failed: {}",
                response.status()
            )));
        }
        
        Ok(())
    }
    
    async fn list(&self, prefix: Option<&str>, pagination: &Pagination) -> Result<ListResult, StorageError> {
        let mut query = String::new();
        query.push_str("list-type=2");
        
        if let Some(p) = prefix {
            query.push_str(&format!("&prefix={}", urlencoding::encode(p)));
        }
        
        if let Some(limit) = pagination.limit {
            query.push_str(&format!("&max-keys={}", limit));
        }
        
        let path = "/";
        
        let response = self.request("GET", &path, &query, None, None).await?;
        
        if !response.status().is_success() {
            return Err(StorageError::ProviderError(format!(
                "List failed: {}",
                response.status()
            )));
        }
        
        let body = response.text().await.map_err(|e| StorageError::NetworkError(e.to_string()))?;
        
        let entries = parse_list_response(&body);
        
        Ok(ListResult {
            entries,
            next_continuation_token: None,
        })
    }
    
    async fn presign(&self, key: &str, expires: std::time::Duration) -> Result<String, StorageError> {
        let expires_secs = expires.as_secs() as i64;
        
        let path = format!("/{}", key);
        let query = format!("X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential={}%2F{}%2F{}%2Fs3%2Faws4_request&X-Amz-Date={}&X-Amz-Expires={}&X-Amz-SignedHeaders=host",
            urlencoding::encode(self.config.access_key.as_ref().unwrap()),
            chrono::Utc::now().format("%Y%m%d").to_string(),
            self.config.region,
            chrono::Utc::now().format("%Y%m%dT%H%M%SZ"),
            expires_secs
        );
        
        let url = format!("{}{}?{}", self.endpoint, path, query);
        
        Ok(url)
    }
    
    async fn head(&self, key: &str) -> Result<FileMetadata, StorageError> {
        let path = format!("/{}", key);
        
        let response = self.request("HEAD", &path, "", None, None).await?;
        
        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(StorageError::NotFound(key.to_string()));
        }
        
        if !response.status().is_success() {
            return Err(StorageError::ProviderError(format!(
                "Head failed: {}",
                response.status()
            )));
        }
        
        Ok(FileMetadata {
            original_name: key.split('/').last().unwrap_or(key).to_string(),
            content_type: response.headers()
                .get("content-type")
                .and_then(|v| v.to_str().ok())
                .map(|s| s.to_string()),
            cache_control: response.headers()
                .get("cache-control")
                .and_then(|v| v.to_str().ok())
                .map(|s| s.to_string()),
            content_disposition: None,
            metadata: std::collections::HashMap::new(),
            expires_after: None,
            is_public: false,
        })
    }
    
    async fn copy(&self, src: &str, dest: &str) -> Result<(), StorageError> {
        let path = format!("/{}", dest);
        let copy_source = format!("/{}/{}", self.bucket, src);
        
        let mut headers = HeaderMap::new();
        headers.insert("x-amz-copy-source", HeaderValue::from_str(&copy_source).unwrap());
        
        let url = format!("{}{}?{}", self.endpoint, path, "");
        
        let response = self.client.put(&url)
            .headers(headers)
            .send()
            .await
            .map_err(|e| StorageError::NetworkError(e.to_string()))?;
        
        if !response.status().is_success() {
            return Err(StorageError::ProviderError(format!(
                "Copy failed: {}",
                response.status()
            )));
        }
        
        Ok(())
    }
    
    async fn move_to(&self, src: &str, dest: &str) -> Result<(), StorageError> {
        self.copy(src, dest).await?;
        self.delete(src).await?;
        Ok(())
    }
    
    fn bucket(&self) -> &str {
        &self.bucket
    }
    
    fn provider_name(&self) -> &str {
        "s3"
    }
}

fn parse_list_response(body: &str) -> Vec<FileEntry> {
    let mut entries = Vec::new();
    
    if let Some(contents_start) = body.find("<Contents>") {
        let contents = &body[contents_start..];
        
        let mut search_pos = 0;
        while let Some(key_start) = contents[search_pos..].find("<Key>") {
            let key_start = search_pos + key_start;
            if let Some(key_end) = contents[key_start..].find("</Key>") {
                let key = &contents[key_start + 5..key_start + key_end];
                
                let size = if let Some(size_start) = contents[key_start..].find("<Size>") {
                    let size_start = key_start + size_start + 7;
                    if let Some(size_end) = contents[size_start..].find("</Size>") {
                        contents[size_start..size_start + size_end].parse().unwrap_or(0)
                    } else {
                        0
                    }
                } else {
                    0
                };
                
                entries.push(FileEntry::new(
                    S3Storage::generate_id(),
                    key.to_string(),
                    size,
                    "application/octet-stream".to_string(),
                ));
                
                search_pos = key_start + key_end + 6;
            } else {
                break;
            }
        }
    }
    
    entries
}

mod urlencoding {
    pub fn encode(s: &str) -> String {
        let mut result = String::new();
        for byte in s.bytes() {
            match byte {
                b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                    result.push(byte as char);
                }
                _ => {
                    result.push_str(&format!("%{:02X}", byte));
                }
            }
        }
        result
    }
}