-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaws.rs
323 lines (256 loc) · 9.65 KB
/
aws.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
//! Module providing an implementation for the [Storage] trait using Amazon's S3 object storage service.
use regex::Regex;
use std::path::{ PathBuf };
use std::time::Duration;
use async_trait::async_trait;
use crate::htsget::Url;
use super::{GetOptions, Result, UrlOptions};
use crate::storage::async_storage::AsyncStorage;
use crate::storage::StorageError::InvalidKey;
use aws_config;
use aws_config::meta::region::RegionProviderChain;
use aws_sdk_s3::input::GetObjectInput;
use aws_sdk_s3::presigning::config::PresigningConfig;
use aws_sdk_s3::{Client, Config, Region};
//use crate::storage::s3_testing::fs_write_object;
enum Retrieval {
Immediate,
Delayed,
}
enum AwsS3StorageTier {
Standard(Retrieval),
StandardIa(Retrieval),
OnezoneIa(Retrieval),
Glacier(Retrieval), // ~24-48 hours
DeepArchive(Retrieval), // ~48 hours
}
/// Implementation for the [Storage] trait using the local file system.
pub struct AwsS3Storage {
client: Client,
bucket: String,
key: String,
}
impl AwsS3Storage {
fn new(client: Client, bucket: String, key: String) -> Self {
AwsS3Storage {
client,
bucket,
key,
}
}
// TODO: Take into account all S3 URL styles...: https://gist.github.com/bh1428/c30b7db493828ece622a6cb38c05362a
async fn get_bucket_and_key_from_s3_url(s3_url: String) -> Result<(String, String)> {
// TODO: Rewrite as match
// match s3_url {
//
// }
if s3_url.starts_with("s3://") {
let re = Regex::new(r"s3://([^/]+)/(.*)").unwrap();
let cap = re.captures(&s3_url).unwrap();
let bucket = cap[1].to_string();
let key = cap[2].to_string();
Ok((bucket, key))
} else if s3_url.starts_with("http://") { // useful for local testing, not for prod
let re = Regex::new(r"http://([^/]+)/(.*)").unwrap();
let cap = re.captures(&s3_url).unwrap();
let bucket = cap[1].to_string();
let key = cap[2].to_string();
Ok((bucket, key))
} else if s3_url.starts_with("https://") {
Err(InvalidKey(s3_url))
} else {
Err(InvalidKey(s3_url))
}
}
async fn s3_presign_url(client: Client, bucket: String, key: String) -> Result<String> {
let expires_in = Duration::from_secs(900);
let region_provider = RegionProviderChain::first_try("ap-southeast-2")
.or_default_provider()
.or_else(Region::new("us-east-1"));
let shared_config = aws_config::from_env().region(region_provider).load().await;
// Presigned requests can be made with the client directly
let presigned_request = client
.get_object()
.bucket(&bucket)
.key(&key)
.presigned(PresigningConfig::expires_in(expires_in).unwrap())
.await;
// Or, they can be made directly from an operation input
let presigned_request = GetObjectInput::builder()
.bucket(bucket)
.key(key)
.build().unwrap()
.presigned(
&Config::from(&shared_config),
PresigningConfig::expires_in(expires_in).unwrap(),
)
.await;
Ok(presigned_request.unwrap().uri().to_string())
}
async fn s3_head(client: Client, bucket: String, key: String) -> Result<u64> {
let content_length = client
.head_object()
.bucket(bucket)
.key(key)
.send()
.await
.unwrap()
.content_length as u64;
dbg!(content_length);
Ok(content_length)
}
async fn get_storage_tier(s3_url: String) -> Result<AwsS3StorageTier> {
// 1. S3 head request to object
// 2. Return status
// Similar (Java) code I wrote here: https://github.com/igvteam/igv/blob/master/src/main/java/org/broad/igv/util/AmazonUtils.java#L257
// Or with AWS cli with: $ aws s3api head-object --bucket awsexamplebucket --key dir1/example.obj
unimplemented!();
}
}
// TODO: Determine if all three trait methods require Retrievavility testing before
// reaching out to actual S3 objects or just the "head" operation.
// i.e: Should we even return a presigned URL if the object is not immediately retrievable?`
#[async_trait]
impl AsyncStorage for AwsS3Storage {
/// Returns the S3 url (s3://bucket/key) for the given path (key).
async fn get<K: AsRef<str> + Send>(&self, key: K, _options: GetOptions) -> Result<PathBuf> {
let key: &str = key.as_ref();
let (bucket, s3key) = AwsS3Storage::get_bucket_and_key_from_s3_url(key.to_string()).await?;
let s3path = PathBuf::from(bucket).join(s3key);
Ok(s3path)
}
/// Returns a S3-presigned htsget URL
async fn url<K: AsRef<str> + Send>(&self, key: K, options: UrlOptions) -> Result<Url> {
let region_provider = RegionProviderChain::first_try("ap-southeast-2")
.or_default_provider()
.or_else(Region::new("us-east-1"));
let shared_config = aws_config::from_env().region(region_provider).load().await;
let client = Client::new(&shared_config);
let presigned_url =
AwsS3Storage::s3_presign_url(client, self.bucket.clone(), key.as_ref().to_string());
let htsget_url = Url::new(presigned_url.await?);
Ok(htsget_url)
}
/// Returns the size of the S3 object in bytes.
async fn head<K: AsRef<str> + Send>(&self, key: K) -> Result<u64> {
let region_provider = RegionProviderChain::first_try("ap-southeast-2")
.or_default_provider()
.or_else(Region::new("us-east-1"));
let shared_config = aws_config::from_env().region(region_provider).load().await;
let key: &str = key.as_ref(); // input URI or path, not S3 key... the trait naming is a bit misleading
let client = Client::new(&shared_config);
let (bucket, s3key) = AwsS3Storage::get_bucket_and_key_from_s3_url(key.to_string()).await?;
let object_bytes = AwsS3Storage::s3_head(client, self.bucket.clone(), s3key).await?;
Ok(object_bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::s3_testing::setup_service;
use crate::storage::s3_testing::recv_body_string;
use crate::storage::s3_testing::fs_write_object;
use hyper::{Body, Method, StatusCode};
use s3_server::headers::HeaderValue;
use s3_server::headers::X_AMZ_CONTENT_SHA256;
type Request = hyper::Request<hyper::Body>;
async fn aws_s3_client() -> Client {
let region_provider = RegionProviderChain::first_try("ap-southeast-2")
.or_default_provider()
.or_else(Region::new("us-east-1"));
let shared_config = aws_config::from_env().region(region_provider).load().await;
Client::new(&shared_config)
}
#[tokio::test]
async fn test_split_s3_url_into_bucket_and_key() {
let s3_url = "s3://bucket/key";
let (bucket, key) = AwsS3Storage::get_bucket_and_key_from_s3_url(s3_url.to_string())
.await
.unwrap();
let s3_storage = AwsS3Storage::new(
aws_s3_client().await,
bucket.clone(),
key.clone(),
);
assert_eq!(bucket, "bucket");
assert_eq!(key, "key");
}
#[tokio::test]
async fn test_get_htsget_url_from_s3() {
let s3_url = "s3://bucket/key";
let (bucket, key) = AwsS3Storage::get_bucket_and_key_from_s3_url(s3_url.to_string())
.await
.unwrap();
let s3_storage = AwsS3Storage::new(
aws_s3_client().await,
bucket.clone(),
key.clone(),
);
let htsget_url = s3_storage.url(key, UrlOptions::default()).await.unwrap();
//dbg!(&htsget_url);
assert!(htsget_url.url.contains("X-Amz-Signature"));
}
// #[tokio::test]
// async fn test_get_head_bytes_from_s3() {
// // Tilt up the local S3 server...
// let (root, service) = setup_service().unwrap();
// let bucket = "asd";
// let key = "qwe";
// let content = "Hello World!";
// fs_write_object(root, bucket, key, content).unwrap();
// let mut req = Request::new(Body::empty());
// *req.method_mut() = Method::GET;
// *req.uri_mut() = format!("http://localhost:8014/{}/{}", bucket, key)
// .parse()
// .unwrap();
// req.headers_mut().insert(
// X_AMZ_CONTENT_SHA256.clone(),
// HeaderValue::from_static("UNSIGNED-PAYLOAD"),
// );
// let mut res = service.hyper_call(req).await.unwrap();
// let body = recv_body_string(&mut res).await.unwrap();
// // TODO: Find an aws_sdk_rust equivalent? Not sure this exists :_S
// // let local_region = Region::Custom {
// // endpoint: "http://localhost:8014".to_owned(),
// // name: "local".to_owned(),
// // };
// let s3_storage = AwsS3Storage::new(
// aws_s3_client().await,
// bucket.to_string(),
// key.to_string(),
// );
// let obj_head = format!("http://localhost:8014/{}/{}", bucket, key);
// //dbg!(&obj_head);
// let htsget_head = s3_storage.head(obj_head).await.unwrap();
// // assert_eq!(res.status(), StatusCode::OK);
// // assert_eq!(body, content);
// }
#[tokio::test]
async fn test_get_local_s3_server_object() {
let (root, service) = setup_service().unwrap();
let bucket = "asd";
let key = "qwe";
let content = "Hello World!";
fs_write_object(root, bucket, key, content).unwrap();
let mut req = Request::new(Body::empty());
*req.method_mut() = Method::GET;
*req.uri_mut() = format!("http://localhost:8014/{}/{}", bucket, key)
.parse()
.unwrap();
req.headers_mut().insert(
X_AMZ_CONTENT_SHA256.clone(),
HeaderValue::from_static("UNSIGNED-PAYLOAD"),
);
let mut res = service.hyper_call(req).await.unwrap();
let body = recv_body_string(&mut res).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(body, content);
}
#[tokio::test]
async fn local_s3_server_returns_htsget_url() {
let (root, service) = setup_service().unwrap();
let bucket = "bucket";
let key = "key";
let content = "Hello World!";
}
}