-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paths3.rs
More file actions
223 lines (193 loc) · 8 KB
/
Copy paths3.rs
File metadata and controls
223 lines (193 loc) · 8 KB
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
use core::fmt::{self, Display};
use std::io::Write;
use anyhow::Context;
use aws_sdk_s3::error::SdkError;
use aws_sdk_s3::operation::get_object::GetObjectError;
use log::info;
use super::{BundleLoader, BundleType};
/// Re-export the `aws-config` crate as a module so that the user
/// does not have to depend on the `aws-cponfig` crate directly
pub mod aws_config {
pub use ::aws_config::*;
}
/// A loader that reads bundles from an S3 bucket and an optional prefix.
///
/// The loader will attempt to load a bundle by ID, or if no ID is provided, it will load the first bundle found in the bucket, by listing
/// the contents of the bucket using an S3 `list_objects_v2` operation.
///
/// The bundle is loaded by reading the object data from the bucket using an S3 `get_object` operation.
/// The object key is constructed as follows:
/// - If the `id` argument is present when calling `load`, then the key is [<optional-prefix>/]<ID>[.<suffix>]
/// where `<suffix>` is one of the suffixes returned by `BundleType::suffix()`, examined in order of the variants of `BundleType`
/// - If the `id` argument is not present when calling `load`, then the loader will list the contents of the bucket and load the first bundle
/// with a suffix matching one of the suffixes returned by `BundleType::suffix()`, examined in order of the variants of `BundleType`
/// Furthermore, if the `delete_after_load` flag is set to `true`, then the loader will delete the loaded bundle from the bucket
#[derive(Debug, Clone)]
pub struct S3Loader {
config: Option<aws_config::SdkConfig>,
load_bucket: String,
load_prefix: Option<String>,
delete_after_load: bool,
#[allow(unused)]
logs_bucket: Option<String>,
#[allow(unused)]
logs_prefix: Option<String>,
}
impl S3Loader {
/// Creates a new `S3Loader` instance
///
/// # Arguments
/// - `load_bucket`: The name of the S3 bucket to load the bundles from
/// - `load_prefix`: An optional prefix key to use when loading the bundles
/// - `delete_after_load`: A flag indicating whether the loaded bundle should be deleted from the bucket after loading
/// - `logs_bucket`: An optional name of the S3 bucket where the logs are uploaded;
/// if provided, the loader will only download a bundle if its logs are not yet uploaded, this preventing
/// flashing a bundle multiple times
/// - `logs_prefix`: An optional prefix key to use when checking for uploaded logs
pub const fn new(
config: Option<aws_config::SdkConfig>,
load_bucket: String,
load_prefix: Option<String>,
delete_after_load: bool,
logs_bucket: Option<String>,
logs_prefix: Option<String>,
) -> Self {
Self {
config,
load_bucket,
load_prefix,
delete_after_load,
logs_bucket,
logs_prefix,
}
}
}
impl BundleLoader for S3Loader {
async fn load<W>(&mut self, mut write: W, id: Option<&str>) -> anyhow::Result<String>
where
W: Write,
{
if let Some(id) = id {
info!(
"About to fetch a bundle with ID `{id}` from S3 bucket `{}`...",
BucketWithPrefix::new(&self.load_bucket, self.load_prefix.as_deref())
);
} else {
info!(
"About to fetch a random bundle from S3 bucket `{}`...",
BucketWithPrefix::new(&self.load_bucket, self.load_prefix.as_deref())
);
}
let config = if let Some(config) = self.config.as_ref() {
config.clone()
} else {
aws_config::load_from_env().await
};
let client = aws_sdk_s3::Client::new(&config);
if let Some(id) = id {
for bundle_type in BundleType::iter() {
let bundle_name = bundle_type.file(id);
let key = self
.load_prefix
.as_deref()
.map(|prefix| format!("{}/{}", prefix, bundle_name))
.unwrap_or(bundle_name.clone());
let result = client
.get_object()
.bucket(&self.load_bucket)
.key(&key)
.send()
.await;
match result {
Ok(mut object_data) => {
if object_data.delete_marker().unwrap_or(false) {
continue;
}
while let Some(bytes) = object_data.body.try_next().await? {
write.write_all(&bytes)?;
}
return Ok(bundle_name);
}
Err(SdkError::ServiceError(err))
if matches!(err.err(), GetObjectError::NoSuchKey(_)) =>
{
continue
}
Err(other) => Err(other).context("Loading the bundle failed")?,
}
}
} else {
let mut continuation_token = None;
loop {
let mut builder = client.list_objects_v2().bucket(&self.load_bucket);
if let Some(continuation_token) = continuation_token {
builder = builder.continuation_token(continuation_token);
}
if let Some(prefix) = &self.load_prefix {
builder = builder.prefix(prefix);
}
let resp = builder.send().await?;
for object_desc in resp.contents() {
if let Some(key) = object_desc.key() {
if BundleType::iter().any(|bundle_type| key.ends_with(bundle_type.suffix()))
{
let mut object_data = client
.get_object()
.bucket(&self.load_bucket)
.key(key)
.send()
.await
.context("Loading the bundle failed")?;
while let Some(bytes) = object_data.body.try_next().await? {
write
.write_all(&bytes)
.context("Loading the bundle failed")?;
}
let bundle_name = key.split('/').next_back().unwrap_or(key).to_string();
if self.delete_after_load {
client
.delete_object()
.bucket(&self.load_bucket)
.key(key)
.send()
.await
.context("Deleting the bundle after loading failed")?;
}
info!("Loaded bundle `{}`", bundle_name);
return Ok(bundle_name);
}
}
}
if let Some(cont) = resp.next_continuation_token() {
continuation_token = Some(cont.to_string());
} else {
break;
}
}
}
if let Some(id) = id {
anyhow::bail!("No bundle found for ID `{id}`")
} else {
anyhow::bail!("No bundles found in the bucket")
}
}
}
#[derive(Debug)]
struct BucketWithPrefix<'a> {
bucket: &'a str,
prefix: Option<&'a str>,
}
impl<'a> BucketWithPrefix<'a> {
const fn new(bucket: &'a str, prefix: Option<&'a str>) -> Self {
Self { bucket, prefix }
}
}
impl Display for BucketWithPrefix<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(prefix) = self.prefix {
write!(f, "{}/{}", self.bucket, prefix)
} else {
write!(f, "{}", self.bucket)
}
}
}