| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
use std::collections::HashMap; |
| 16 |
use std::sync::{Arc, Mutex}; |
| 17 |
|
| 18 |
use axum::{ |
| 19 |
Router, |
| 20 |
body::Bytes, |
| 21 |
extract::{Path, State}, |
| 22 |
http::{HeaderMap, StatusCode, header}, |
| 23 |
response::{IntoResponse, Response}, |
| 24 |
routing::any, |
| 25 |
}; |
| 26 |
|
| 27 |
|
| 28 |
#[derive(Clone)] |
| 29 |
pub(crate) struct StoredObject { |
| 30 |
pub data: Vec<u8>, |
| 31 |
pub content_type: String, |
| 32 |
} |
| 33 |
|
| 34 |
type Objects = Arc<Mutex<HashMap<String, StoredObject>>>; |
| 35 |
|
| 36 |
|
| 37 |
pub(crate) struct S3Stub { |
| 38 |
pub endpoint: String, |
| 39 |
pub bucket: String, |
| 40 |
objects: Objects, |
| 41 |
} |
| 42 |
|
| 43 |
impl S3Stub { |
| 44 |
|
| 45 |
pub(crate) async fn start() -> Self { |
| 46 |
let objects: Objects = Arc::new(Mutex::new(HashMap::new())); |
| 47 |
let bucket = "test-bucket".to_string(); |
| 48 |
|
| 49 |
let app = Router::new() |
| 50 |
.route("/{bucket}/{*key}", any(object_handler)) |
| 51 |
.with_state(objects.clone()); |
| 52 |
|
| 53 |
let listener = tokio::net::TcpListener::bind("127.0.0.1:0") |
| 54 |
.await |
| 55 |
.expect("failed to bind S3 stub"); |
| 56 |
let addr = listener.local_addr().expect("no local addr for S3 stub"); |
| 57 |
|
| 58 |
tokio::spawn(async move { |
| 59 |
let _ = axum::serve(listener, app).await; |
| 60 |
}); |
| 61 |
|
| 62 |
S3Stub { |
| 63 |
endpoint: format!("http://{addr}"), |
| 64 |
bucket, |
| 65 |
objects, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
pub(crate) fn len(&self) -> usize { |
| 71 |
self.objects.lock().expect("S3 stub mutex poisoned").len() |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
pub(crate) fn get(&self, key: &str) -> Option<StoredObject> { |
| 76 |
self.objects |
| 77 |
.lock() |
| 78 |
.expect("S3 stub mutex poisoned") |
| 79 |
.get(key) |
| 80 |
.cloned() |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
async fn object_handler( |
| 87 |
State(objects): State<Objects>, |
| 88 |
Path((_bucket, key)): Path<(String, String)>, |
| 89 |
method: axum::http::Method, |
| 90 |
headers: HeaderMap, |
| 91 |
body: Bytes, |
| 92 |
) -> Response { |
| 93 |
match method { |
| 94 |
axum::http::Method::PUT => { |
| 95 |
let content_type = headers |
| 96 |
.get(header::CONTENT_TYPE) |
| 97 |
.and_then(|v| v.to_str().ok()) |
| 98 |
.unwrap_or("application/octet-stream") |
| 99 |
.to_string(); |
| 100 |
objects.lock().expect("S3 stub mutex poisoned").insert( |
| 101 |
key, |
| 102 |
StoredObject { |
| 103 |
data: body.to_vec(), |
| 104 |
content_type, |
| 105 |
}, |
| 106 |
); |
| 107 |
|
| 108 |
([(header::ETAG, "\"stub\"")], StatusCode::OK).into_response() |
| 109 |
} |
| 110 |
axum::http::Method::GET => { |
| 111 |
let found = objects |
| 112 |
.lock() |
| 113 |
.expect("S3 stub mutex poisoned") |
| 114 |
.get(&key) |
| 115 |
.cloned(); |
| 116 |
match found { |
| 117 |
Some(obj) => ( |
| 118 |
[ |
| 119 |
(header::CONTENT_TYPE, obj.content_type), |
| 120 |
(header::ETAG, "\"stub\"".to_string()), |
| 121 |
], |
| 122 |
obj.data, |
| 123 |
) |
| 124 |
.into_response(), |
| 125 |
None => not_found(), |
| 126 |
} |
| 127 |
} |
| 128 |
axum::http::Method::DELETE => { |
| 129 |
objects.lock().expect("S3 stub mutex poisoned").remove(&key); |
| 130 |
StatusCode::NO_CONTENT.into_response() |
| 131 |
} |
| 132 |
_ => StatusCode::METHOD_NOT_ALLOWED.into_response(), |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
fn not_found() -> Response { |
| 139 |
( |
| 140 |
StatusCode::NOT_FOUND, |
| 141 |
[(header::CONTENT_TYPE, "application/xml")], |
| 142 |
r#"<?xml version="1.0" encoding="UTF-8"?><Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message></Error>"#, |
| 143 |
) |
| 144 |
.into_response() |
| 145 |
} |
| 146 |
|