forked from umccr/htsget-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.rs
415 lines (375 loc) · 11.8 KB
/
s3.rs
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
//! Module providing an implementation for the [Storage] trait using Amazon's S3 object storage service.
//!
use std::fmt::Debug;
use std::io;
use std::io::ErrorKind::Other;
use std::time::Duration;
use async_trait::async_trait;
use aws_sdk_s3::error::SdkError;
use aws_sdk_s3::operation::get_object::builders::GetObjectFluentBuilder;
use aws_sdk_s3::operation::get_object::GetObjectError;
use aws_sdk_s3::operation::head_object::{HeadObjectError, HeadObjectOutput};
use aws_sdk_s3::presigning::PresigningConfig;
use aws_sdk_s3::primitives::{ByteStream, SdkBody};
use aws_sdk_s3::types::StorageClass;
use aws_sdk_s3::Client;
use bytes::Bytes;
use http::Response;
use tokio_util::io::StreamReader;
use tracing::debug;
use tracing::instrument;
use crate::storage::s3::Retrieval::{Delayed, Immediate};
use crate::storage::StorageError::{AwsS3Error, KeyNotFound};
use crate::storage::{BytesPosition, HeadOptions, StorageError};
use crate::storage::{BytesRange, Storage};
use crate::Url;
use super::{GetOptions, RangeUrlOptions, Result};
/// Represents data classes that can be retrieved immediately or after a delay.
/// Specifically, Glacier Flexible, Glacier Deep Archive, and Intelligent Tiering archive
/// tiers have delayed retrieval, unless they have been restored.
#[derive(Debug)]
pub enum Retrieval {
Immediate(StorageClass),
Delayed(StorageClass),
}
/// Implementation for the [Storage] trait utilising data from an S3 bucket.
#[derive(Debug, Clone)]
pub struct S3Storage {
client: Client,
bucket: String,
}
impl S3Storage {
// Allow the user to set this?
pub const PRESIGNED_REQUEST_EXPIRY: u64 = 1000;
pub fn new(client: Client, bucket: String) -> Self {
S3Storage { client, bucket }
}
pub async fn new_with_default_config(
bucket: String,
endpoint: Option<String>,
path_style: bool,
) -> Self {
let sdk_config = aws_config::load_from_env().await;
let mut s3_config_builder = aws_sdk_s3::config::Builder::from(&sdk_config);
s3_config_builder.set_endpoint_url(endpoint); // For local S3 storage, i.e: Minio
s3_config_builder.set_force_path_style(Some(path_style));
let client = s3_config_builder.build();
let s3_client = Client::from_conf(client);
S3Storage::new(s3_client, bucket)
}
/// Return an S3 pre-signed URL of the key. This function does not check that the key exists,
/// so this should be checked before calling it.
pub async fn s3_presign_url<K: AsRef<str> + Send>(
&self,
key: K,
range: &BytesPosition,
) -> Result<String> {
let response = self
.client
.get_object()
.bucket(&self.bucket)
.key(key.as_ref());
let response = Self::apply_range(response, range);
Ok(
response
.presigned(
PresigningConfig::expires_in(Duration::from_secs(Self::PRESIGNED_REQUEST_EXPIRY))
.map_err(|err| AwsS3Error(err.to_string(), key.as_ref().to_string()))?,
)
.await
.map_err(|err| Self::map_get_error(key, err))?
.uri()
.to_string(),
)
}
async fn s3_head<K: AsRef<str> + Send>(&self, key: K) -> Result<HeadObjectOutput> {
self
.client
.head_object()
.bucket(&self.bucket)
.key(key.as_ref())
.send()
.await
.map_err(|err| {
let err = err.into_service_error();
if let HeadObjectError::NotFound(_) = err {
KeyNotFound(key.as_ref().to_string())
} else {
AwsS3Error(err.to_string(), key.as_ref().to_string())
}
})
}
/// Returns the retrieval type of the object stored with the key.
#[instrument(level = "trace", skip_all, ret)]
pub async fn get_retrieval_type<K: AsRef<str> + Send>(&self, key: K) -> Result<Retrieval> {
let head = self.s3_head(key.as_ref()).await?;
Ok(
// Default is Standard.
match head.storage_class.unwrap_or(StorageClass::Standard) {
class @ (StorageClass::DeepArchive | StorageClass::Glacier) => {
Self::check_restore_header(head.restore, class)
}
class @ StorageClass::IntelligentTiering => {
if head.archive_status.is_some() {
// Not sure if this check is necessary for the archived intelligent tiering classes but
// it shouldn't hurt.
Self::check_restore_header(head.restore, class)
} else {
Immediate(class)
}
}
class => Immediate(class),
},
)
}
fn check_restore_header(restore_header: Option<String>, class: StorageClass) -> Retrieval {
if let Some(restore) = restore_header {
if restore.contains("ongoing-request=\"false\"") {
return Immediate(class);
}
}
Delayed(class)
}
fn apply_range(builder: GetObjectFluentBuilder, range: &BytesPosition) -> GetObjectFluentBuilder {
let range: String = String::from(&BytesRange::from(range));
if range.is_empty() {
builder
} else {
builder.range(range)
}
}
/// Get the key from S3 storage as a `ByteStream`.
pub async fn get_content<K: AsRef<str> + Send>(
&self,
key: K,
options: GetOptions<'_>,
) -> Result<ByteStream> {
if let Delayed(class) = self.get_retrieval_type(key.as_ref()).await? {
return Err(AwsS3Error(
format!("cannot retrieve object immediately, class is `{class:?}`"),
key.as_ref().to_string(),
));
}
let response = self
.client
.get_object()
.bucket(&self.bucket)
.key(key.as_ref());
let response = Self::apply_range(response, options.range());
Ok(
response
.send()
.await
.map_err(|err| Self::map_get_error(key, err))?
.body,
)
}
async fn create_stream_reader<K: AsRef<str> + Send>(
&self,
key: K,
options: GetOptions<'_>,
) -> Result<StreamReader<ByteStream, Bytes>> {
let response = self.get_content(key, options).await?;
Ok(StreamReader::new(response))
}
fn map_get_error<K>(key: K, error: SdkError<GetObjectError, Response<SdkBody>>) -> StorageError
where
K: AsRef<str> + Send,
{
let error = error.into_service_error();
if let GetObjectError::NoSuchKey(_) = error {
KeyNotFound(key.as_ref().to_string())
} else {
AwsS3Error(error.to_string(), key.as_ref().to_string())
}
}
}
#[async_trait]
impl Storage for S3Storage {
type Streamable = StreamReader<ByteStream, Bytes>;
/// Gets the actual s3 object as a buffered reader.
#[instrument(level = "trace", skip(self))]
async fn get<K: AsRef<str> + Send + Debug>(
&self,
key: K,
options: GetOptions<'_>,
) -> Result<Self::Streamable> {
let key = key.as_ref();
debug!(calling_from = ?self, key, "getting file with key {:?}", key);
self.create_stream_reader(key, options).await
}
/// Return an S3 pre-signed htsget URL. This function does not check that the key exists, so this
/// should be checked before calling it.
#[instrument(level = "trace", skip(self))]
async fn range_url<K: AsRef<str> + Send + Debug>(
&self,
key: K,
options: RangeUrlOptions<'_>,
) -> Result<Url> {
let key = key.as_ref();
let presigned_url = self.s3_presign_url(key, options.range()).await?;
let url = options.apply(Url::new(presigned_url));
debug!(calling_from = ?self, key, ?url, "getting url with key {:?}", key);
Ok(url)
}
/// Returns the size of the S3 object in bytes.
#[instrument(level = "trace", skip(self))]
async fn head<K: AsRef<str> + Send + Debug>(
&self,
key: K,
_options: HeadOptions<'_>,
) -> Result<u64> {
let key = key.as_ref();
let head = self.s3_head(key).await?;
let len = u64::try_from(head.content_length).map_err(|err| {
StorageError::IoError(
"failed to convert file length to `u64`".to_string(),
io::Error::new(Other, err),
)
})?;
debug!(calling_from = ?self, key, len, "size of key {:?} is {}", key, len);
Ok(len)
}
}
#[cfg(test)]
pub(crate) mod tests {
use std::future::Future;
use std::path::Path;
use std::sync::Arc;
use htsget_test::aws_mocks::with_s3_test_server;
use crate::storage::local::tests::create_local_test_files;
use crate::storage::s3::S3Storage;
use crate::storage::{BytesPosition, GetOptions, RangeUrlOptions, Storage};
use crate::storage::{HeadOptions, StorageError};
use crate::Headers;
pub(crate) async fn with_aws_s3_storage_fn<F, Fut>(test: F, folder_name: String, base_path: &Path)
where
F: FnOnce(Arc<S3Storage>) -> Fut,
Fut: Future<Output = ()>,
{
with_s3_test_server(base_path, |client| async move {
test(Arc::new(S3Storage::new(client, folder_name))).await;
})
.await;
}
async fn with_aws_s3_storage<F, Fut>(test: F)
where
F: FnOnce(Arc<S3Storage>) -> Fut,
Fut: Future<Output = ()>,
{
let (folder_name, base_path) = create_local_test_files().await;
with_aws_s3_storage_fn(test, folder_name, base_path.path()).await;
}
#[tokio::test]
async fn existing_key() {
with_aws_s3_storage(|storage| async move {
let result = storage
.get(
"key2",
GetOptions::new_with_default_range(&Default::default()),
)
.await;
assert!(result.is_ok());
})
.await;
}
#[tokio::test]
async fn non_existing_key() {
with_aws_s3_storage(|storage| async move {
let result = storage
.get(
"non-existing-key",
GetOptions::new_with_default_range(&Default::default()),
)
.await;
assert!(matches!(result, Err(StorageError::AwsS3Error(_, _))));
})
.await;
}
#[tokio::test]
async fn url_of_existing_key() {
with_aws_s3_storage(|storage| async move {
let result = storage
.range_url(
"key2",
RangeUrlOptions::new_with_default_range(&Default::default()),
)
.await
.unwrap();
assert!(result.url.starts_with("http://folder.localhost:8014/key2"));
assert!(result.url.contains(&format!(
"Amz-Expires={}",
S3Storage::PRESIGNED_REQUEST_EXPIRY
)));
})
.await;
}
#[tokio::test]
async fn url_with_specified_range() {
with_aws_s3_storage(|storage| async move {
let result = storage
.range_url(
"key2",
RangeUrlOptions::new(
BytesPosition::new(Some(7), Some(9), None),
&Default::default(),
),
)
.await
.unwrap();
assert!(result.url.starts_with("http://folder.localhost:8014/key2"));
assert!(result.url.contains(&format!(
"Amz-Expires={}",
S3Storage::PRESIGNED_REQUEST_EXPIRY
)));
assert!(result.url.contains("range"));
assert_eq!(
result.headers,
Some(Headers::default().with_header("Range", "bytes=7-8"))
);
})
.await;
}
#[tokio::test]
async fn url_with_specified_open_ended_range() {
with_aws_s3_storage(|storage| async move {
let result = storage
.range_url(
"key2",
RangeUrlOptions::new(BytesPosition::new(Some(7), None, None), &Default::default()),
)
.await
.unwrap();
assert!(result.url.starts_with("http://folder.localhost:8014/key2"));
assert!(result.url.contains(&format!(
"Amz-Expires={}",
S3Storage::PRESIGNED_REQUEST_EXPIRY
)));
assert!(result.url.contains("range"));
assert_eq!(
result.headers,
Some(Headers::default().with_header("Range", "bytes=7-"))
);
})
.await;
}
#[tokio::test]
async fn file_size() {
with_aws_s3_storage(|storage| async move {
let result = storage
.head("key2", HeadOptions::new(&Default::default()))
.await;
let expected: u64 = 6;
assert!(matches!(result, Ok(size) if size == expected));
})
.await;
}
#[tokio::test]
async fn retrieval_type() {
with_aws_s3_storage(|storage| async move {
let result = storage.get_retrieval_type("key2").await;
println!("{result:?}");
})
.await;
}
}