-
Notifications
You must be signed in to change notification settings - Fork 485
/
writer.rs
190 lines (159 loc) · 5.66 KB
/
writer.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::sync::Arc;
use bytes::Buf;
use http::StatusCode;
use super::core::*;
use super::error::from_s3_error;
use super::error::parse_error;
use super::error::S3Error;
use crate::raw::*;
use crate::*;
pub type S3Writers = oio::MultipartWriter<S3Writer>;
pub struct S3Writer {
core: Arc<S3Core>,
op: OpWrite,
path: String,
}
impl S3Writer {
pub fn new(core: Arc<S3Core>, path: &str, op: OpWrite) -> Self {
S3Writer {
core,
path: path.to_string(),
op,
}
}
}
impl oio::MultipartWrite for S3Writer {
async fn write_once(&self, size: u64, body: Buffer) -> Result<()> {
let mut req = self
.core
.s3_put_object_request(&self.path, Some(size), &self.op, body)?;
self.core.sign(&mut req).await?;
let resp = self.core.send(req).await?;
let status = resp.status();
match status {
StatusCode::CREATED | StatusCode::OK => Ok(()),
_ => Err(parse_error(resp)),
}
}
async fn initiate_part(&self) -> Result<String> {
let resp = self
.core
.s3_initiate_multipart_upload(&self.path, &self.op)
.await?;
let status = resp.status();
match status {
StatusCode::OK => {
let bs = resp.into_body();
let result: InitiateMultipartUploadResult =
quick_xml::de::from_reader(bs.reader()).map_err(new_xml_deserialize_error)?;
Ok(result.upload_id)
}
_ => Err(parse_error(resp)),
}
}
async fn write_part(
&self,
upload_id: &str,
part_number: usize,
size: u64,
body: Buffer,
) -> Result<oio::MultipartPart> {
// AWS S3 requires part number must between [1..=10000]
let part_number = part_number + 1;
let checksum = self.core.calculate_checksum(&body);
let mut req = self.core.s3_upload_part_request(
&self.path,
upload_id,
part_number,
size,
body,
checksum.clone(),
)?;
self.core.sign(&mut req).await?;
let resp = self.core.send(req).await?;
let status = resp.status();
match status {
StatusCode::OK => {
let etag = parse_etag(resp.headers())?
.ok_or_else(|| {
Error::new(
ErrorKind::Unexpected,
"ETag not present in returning response",
)
})?
.to_string();
Ok(oio::MultipartPart {
part_number,
etag,
checksum,
})
}
_ => Err(parse_error(resp)),
}
}
async fn complete_part(&self, upload_id: &str, parts: &[oio::MultipartPart]) -> Result<()> {
let parts = parts
.iter()
.map(|p| match &self.core.checksum_algorithm {
None => CompleteMultipartUploadRequestPart {
part_number: p.part_number,
etag: p.etag.clone(),
..Default::default()
},
Some(checksum_algorithm) => match checksum_algorithm {
ChecksumAlgorithm::Crc32c => CompleteMultipartUploadRequestPart {
part_number: p.part_number,
etag: p.etag.clone(),
checksum_crc32c: p.checksum.clone(),
},
},
})
.collect();
let resp = self
.core
.s3_complete_multipart_upload(&self.path, upload_id, parts)
.await?;
let status = resp.status();
match status {
StatusCode::OK => {
// still check if there is any error because S3 might return error for status code 200
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html#API_CompleteMultipartUpload_Example_4
let (parts, body) = resp.into_parts();
let maybe_error: S3Error =
quick_xml::de::from_reader(body.reader()).map_err(new_xml_deserialize_error)?;
if !maybe_error.code.is_empty() {
return Err(from_s3_error(maybe_error, parts));
}
Ok(())
}
_ => Err(parse_error(resp)),
}
}
async fn abort_part(&self, upload_id: &str) -> Result<()> {
let resp = self
.core
.s3_abort_multipart_upload(&self.path, upload_id)
.await?;
match resp.status() {
// s3 returns code 204 if abort succeeds.
StatusCode::NO_CONTENT => Ok(()),
_ => Err(parse_error(resp)),
}
}
}