-
Notifications
You must be signed in to change notification settings - Fork 157
/
put_object.rs
399 lines (349 loc) · 15.3 KB
/
put_object.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
use std::ffi::OsString;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use crate::object_client::{
ObjectClientResult, PutObjectError, PutObjectParams, PutObjectRequest, PutObjectResult, PutObjectSingleParams,
};
use async_trait::async_trait;
use futures::channel::oneshot::{self, Receiver};
use mountpoint_s3_crt::http::request_response::{Header, Headers, HeadersError};
use mountpoint_s3_crt::io::stream::InputStream;
use mountpoint_s3_crt::s3::client::{ChecksumConfig, RequestType, UploadReview};
use thiserror::Error;
use tracing::error;
use super::{
emit_throughput_metric, ETag, PutObjectTrailingChecksums, S3CrtClient, S3CrtClientInner, S3HttpRequest, S3Message,
S3Operation, S3RequestError,
};
const ETAG_HEADER_NAME: &str = "ETag";
const SSE_TYPE_HEADER_NAME: &str = "x-amz-server-side-encryption";
const SSE_KEY_ID_HEADER_NAME: &str = "x-amz-server-side-encryption-aws-kms-key-id";
impl S3CrtClient {
pub(super) async fn put_object(
&self,
bucket: &str,
key: &str,
params: &PutObjectParams,
) -> ObjectClientResult<S3PutObjectRequest, PutObjectError, S3RequestError> {
let span = request_span!(self.inner, "put_object", bucket, key);
let mut message = self.new_put_request(
bucket,
key,
params.storage_class.as_deref(),
params.server_side_encryption.as_deref(),
params.ssekms_key_id.as_deref(),
)?;
let checksum_config = match params.trailing_checksums {
PutObjectTrailingChecksums::Enabled => Some(ChecksumConfig::trailing_crc32c()),
PutObjectTrailingChecksums::ReviewOnly => Some(ChecksumConfig::upload_review_crc32c()),
PutObjectTrailingChecksums::Disabled => None,
};
message.set_checksum_config(checksum_config);
for (name, value) in ¶ms.object_metadata {
message
.set_header(&Header::new(format!("x-amz-meta-{}", name), value))
.map_err(S3RequestError::construction_failure)?
}
for (name, value) in ¶ms.custom_headers {
message
.inner
.add_header(&Header::new(name, value))
.map_err(S3RequestError::construction_failure)?;
}
let review_callback = ReviewCallbackBox::default();
let callback = review_callback.clone();
let (on_headers, response_headers) = response_headers_handler();
let mut options = S3CrtClientInner::new_meta_request_options(message, S3Operation::PutObject);
options.send_using_async_writes(true);
options.on_upload_review(move |review| callback.invoke(review));
options.part_size(self.inner.write_part_size as u64);
// Before the first write, we need to await for the multi-part upload to be created, so we can report errors.
// To do so, we need to detect one of two events (whichever comes first):
// * a CreateMultipartUpload request completes successfully (potentially after a number of retries),
// * the meta-request fails.
let (mpu_created_sender, mpu_created) = oneshot::channel();
let on_mpu_created_sender = Arc::new(Mutex::new(Some(mpu_created_sender)));
let on_error_sender = on_mpu_created_sender.clone();
let body = self.inner.make_simple_http_request_from_options(
options,
span,
move |metrics| {
if metrics.request_type() == RequestType::CreateMultipartUpload && !metrics.error().is_err() {
// Signal that a CreateMultipartUpload completed successfully (unless the meta-request had already failed).
if let Some(sender) = on_mpu_created_sender.lock().unwrap().take() {
_ = sender.send(Ok(()));
}
}
},
move |result| {
// Signal that the meta-request failed (unless a CreateMultipartUpload had already completed successfully).
if let Some(sender) = on_error_sender.lock().unwrap().take() {
_ = sender.send(Err(result.crt_error.into()));
}
None
},
on_headers,
)?;
Ok(S3PutObjectRequest {
body,
review_callback,
start_time: Instant::now(),
total_bytes: 0,
response_headers,
state: S3PutObjectRequestState::CreatingMPU(mpu_created),
})
}
pub(super) async fn put_object_single<'a>(
&self,
bucket: &str,
key: &str,
params: &PutObjectSingleParams,
contents: impl AsRef<[u8]> + Send + 'a,
) -> ObjectClientResult<PutObjectResult, PutObjectError, S3RequestError> {
let span = request_span!(self.inner, "put_object_single", bucket, key);
let start_time = Instant::now();
let (on_headers, response_headers) = response_headers_handler();
let slice = contents.as_ref();
let content_length = slice.len();
let body = {
let mut message = self.new_put_request(
bucket,
key,
params.storage_class.as_deref(),
params.server_side_encryption.as_deref(),
params.ssekms_key_id.as_deref(),
)?;
message
.set_content_length_header(content_length)
.map_err(S3RequestError::construction_failure)?;
if let Some(checksum) = ¶ms.checksum {
message
.set_checksum_header(checksum)
.map_err(S3RequestError::construction_failure)?;
}
for (name, value) in ¶ms.object_metadata {
message
.set_header(&Header::new(format!("x-amz-meta-{}", name), value))
.map_err(S3RequestError::construction_failure)?
}
for (name, value) in ¶ms.custom_headers {
message
.inner
.add_header(&Header::new(name, value))
.map_err(S3RequestError::construction_failure)?;
}
let body_input_stream =
InputStream::new_from_slice(&self.inner.allocator, slice).map_err(S3RequestError::CrtError)?;
message.set_body_stream(Some(body_input_stream));
let options = S3CrtClientInner::new_meta_request_options(message, S3Operation::PutObjectSingle);
self.inner
.make_simple_http_request_from_options(options, span, |_| {}, |_| None, on_headers)?
};
body.await?;
let elapsed = start_time.elapsed();
emit_throughput_metric(content_length as u64, elapsed, "put_object_single");
Ok(extract_result(response_headers.await.expect(
"headers should be available since the request completed successfully",
))?)
}
fn new_put_request(
&self,
bucket: &str,
key: &str,
storage_class: Option<&str>,
server_side_encryption: Option<&str>,
ssekms_key_id: Option<&str>,
) -> Result<S3Message<'_>, S3RequestError> {
let mut message = self
.inner
.new_request_template("PUT", bucket)
.map_err(S3RequestError::construction_failure)?;
let key = format!("/{key}");
message
.set_request_path(&key)
.map_err(S3RequestError::construction_failure)?;
if let Some(storage_class) = storage_class {
message
.set_header(&Header::new("x-amz-storage-class", storage_class))
.map_err(S3RequestError::construction_failure)?;
}
if let Some(sse) = server_side_encryption {
message
.set_header(&Header::new(SSE_TYPE_HEADER_NAME, sse))
.map_err(S3RequestError::construction_failure)?;
}
if let Some(key_id) = ssekms_key_id {
message
.set_header(&Header::new(SSE_KEY_ID_HEADER_NAME, key_id))
.map_err(S3RequestError::construction_failure)?;
}
Ok(message)
}
}
type ReviewCallback = dyn FnOnce(UploadReview) -> bool + Send;
/// Holder for the upload review callback.
/// Used to set the callback when initiating the PutObject request on the CRT client,
/// but redirects to the actual callback the user can specify at completion time.
#[derive(Clone, Default)]
struct ReviewCallbackBox {
callback: Arc<Mutex<Option<Box<ReviewCallback>>>>,
}
impl std::fmt::Debug for ReviewCallbackBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ReviewCallbackBox").finish()
}
}
impl ReviewCallbackBox {
fn set(&mut self, callback: impl FnOnce(UploadReview) -> bool + Send + 'static) {
let previous = self.callback.lock().unwrap().replace(Box::new(callback));
assert!(previous.is_none(), "review callback set twice");
}
fn invoke(self, review: UploadReview) -> bool {
let mut callback = self.callback.lock().unwrap();
let Some(callback) = callback.take() else {
error!("review callback was either never set or invoked twice");
return false;
};
(callback)(review)
}
}
/// An in-progress streaming PutObject request to S3.
///
/// You can write to or complete the upload using the [`PutObjectRequest`] implementation on this
/// object.
#[derive(Debug)]
pub struct S3PutObjectRequest {
body: S3HttpRequest<Vec<u8>, PutObjectError>,
review_callback: ReviewCallbackBox,
start_time: Instant,
total_bytes: u64,
/// Future for the headers of the CompleteMultipartUpload response.
/// Guaranteed to be available after the request finishes successfully.
response_headers: Receiver<Headers>,
state: S3PutObjectRequestState,
}
/// Internal state for a [S3PutObjectRequest].
#[derive(Debug)]
enum S3PutObjectRequestState {
/// Initial state indicating that CreateMultipartUpload may still be in progress. To be awaited on first
/// write so errors can be reported early. The signal indicates that CreateMultipartUpload completed
/// successfully, or that the MPU failed.
CreatingMPU(oneshot::Receiver<Result<(), S3RequestError>>),
/// A write operation is in progress or was interrupted before completion.
PendingWrite,
/// Idle state between write calls.
Idle,
}
fn try_get_header_value(headers: &Headers, key: &str) -> Option<String> {
headers.get(key).ok()?.value().clone().into_string().ok()
}
fn get_etag(response_headers: &Headers) -> Result<ETag, ParseError> {
Ok(response_headers
.get(ETAG_HEADER_NAME)?
.value()
.clone()
.into_string()
.map_err(ParseError::Invalid)?
.into())
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ParseError {
#[error("Header response error: {0}")]
Header(#[from] HeadersError),
#[error("Header string was not valid: {0:?}")]
Invalid(OsString),
}
fn extract_result(response_headers: Headers) -> Result<PutObjectResult, S3RequestError> {
let etag = get_etag(&response_headers).map_err(|e| S3RequestError::InternalError(Box::new(e)))?;
Ok(PutObjectResult {
etag,
sse_type: try_get_header_value(&response_headers, SSE_TYPE_HEADER_NAME),
sse_kms_key_id: try_get_header_value(&response_headers, SSE_KEY_ID_HEADER_NAME),
})
}
/// Creates `on_headers` callback that will send the response headers to the matching `Receiver`.
fn response_headers_handler() -> (impl FnMut(&Headers, i32), Receiver<Headers>) {
let (response_headers_sender, response_headers) = oneshot::channel();
// The callback signature (`FnMut`) allows for it to be invoked multiple times,
// but for PUT requests it will only be called once (on CompleteMultipartUpload
// or on PutObject).
// Wrapping the `oneshot::Sender` in an `Option` allows it to be consumed
// on the first (and only!) invocation.
let mut response_headers_sender = Some(response_headers_sender);
let on_headers = move |headers: &Headers, _: i32| {
if let Some(sender) = response_headers_sender.take() {
let _ = sender.send(headers.clone());
}
};
(on_headers, response_headers)
}
#[cfg_attr(not(docsrs), async_trait)]
impl PutObjectRequest for S3PutObjectRequest {
type ClientError = S3RequestError;
async fn write(&mut self, slice: &[u8]) -> ObjectClientResult<(), PutObjectError, Self::ClientError> {
// Writing to the meta request may require multiple calls. Set the internal
// state to `PendingWrite` until we are done.
match std::mem::replace(&mut self.state, S3PutObjectRequestState::PendingWrite) {
S3PutObjectRequestState::CreatingMPU(create_mpu) => {
// On first write, check the pending CreateMultipartUpload so we can report errors.
// Wait for CreateMultipartUpload to complete successfully, or the MPU to fail.
create_mpu.await.unwrap()?;
}
S3PutObjectRequestState::PendingWrite => {
// Fail if a previous write was not completed.
return Err(S3RequestError::RequestCanceled.into());
}
S3PutObjectRequestState::Idle => {}
}
let meta_request = &mut self.body.meta_request;
let mut slice = slice;
while !slice.is_empty() {
// Write will fail if the request has already finished (because of an error).
let remaining = meta_request
.write(slice, false)
.await
.map_err(S3RequestError::CrtError)?;
self.total_bytes += (slice.len() - remaining.len()) as u64;
slice = remaining;
}
// Write completed with no errors, we can reset to `Idle`.
self.state = S3PutObjectRequestState::Idle;
Ok(())
}
async fn complete(self) -> ObjectClientResult<PutObjectResult, PutObjectError, Self::ClientError> {
self.review_and_complete(|_| true).await
}
async fn review_and_complete(
mut self,
review_callback: impl FnOnce(UploadReview) -> bool + Send + 'static,
) -> ObjectClientResult<PutObjectResult, PutObjectError, Self::ClientError> {
// No need to check for `CreatingMPU`: errors will be reported on completing the upload.
if matches!(self.state, S3PutObjectRequestState::PendingWrite) {
// Fail if a previous write was not completed.
return Err(S3RequestError::RequestCanceled.into());
}
self.review_callback.set(review_callback);
// Write will fail if the request has already finished (because of an error).
_ = self
.body
.meta_request
.write(&[], true)
.await
.map_err(S3RequestError::CrtError)?;
// Now wait for the request to finish.
let _ = self.body.await?;
let elapsed = self.start_time.elapsed();
emit_throughput_metric(self.total_bytes, elapsed, "put_object");
Ok(extract_result(self.response_headers.await.expect(
"headers should be available since the request completed successfully",
))?)
}
}
impl S3PutObjectRequest {
/// The number of bytes written to this request so far.
// TODO: consider exposing on the `PutObjectRequest` trait.
pub fn bytes_written(&self) -> u64 {
self.total_bytes
}
}